473,395 Members | 1,885 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

A function that returns a pointer...??

Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(char *chName) //chName is the name i want to look
for
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names
{
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}
A correct call would be ...???
Student *s = find(NameToFind);
if(s!=NULL)
//do something....
Thx,
Tommy
Jul 22 '05 #1
5 1627

"Tommy Lang" <mu*****@yahoo.se> wrote in message
news:78**************************@posting.google.c om...
Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(char *chName) //chName is the name i want to look
for
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names
Means: if (char * == char *)

That would be true if both strings WERE the same, I mean:
char name[] = "word";

char *test = name;

if (test == name) { //true

You probably want to use strcmp, look it up.

Or better yet, use std:string

if(strcmp(name,chName) == 0) { //Compare names

Watch out for whitespace too.

{
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}



A correct call would be ...???
Student *s = find(NameToFind);
if(s!=NULL)
//do something....
Thx,
Tommy

Jul 22 '05 #2
mu*****@yahoo.se (Tommy Lang) wrote in message news:<78**************************@posting.google. com>...

Your code seems to be okay other than the name comparision. You should
use strcmp instead of using a double equals operator. But I have a few
suggestions to improve your code: Follows
Hi!

I am trying to write a function that goes through an array of
objects(class Student) and checks if
object name(student name)matches search string(char *). I want the
function to return a pointer to the object, if a match is found.

This is my code so far...(am I even close? :-))

//I want to return a ponter of type student
Student *List::find(char *chName) //chName is the name i want to look
When you are using C++, it's always better to use std::string than
using char* . std::string is a container of char* and provides you
lots of facilities and will free you from dealing with the pointer
stuff.
for
{
//Check whole array
for(int i=0;i<100;i++) Where did you get the upper limit of the interation; 100, from? Are
you limiting your list to contain just 100 students? I guess this
problem is because of the usage of arrays (I guess the below MyArray
stuff). Here goes the second suggestion: Always prefer using a
std::vector or anyother container rather than using arrays. This is a
strong suggestion. If you would have used a vector like
std::vector<Student*> m_vecStudents, you can get the size of the
vector using vector.size(). This would help you in not letting an
upper limit to your List size. {
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names Use strcmp for the comparision. If you use std::string s, then you can
use the double equals operator for the comparision. {
return temp; //return pointer to object if found
}
}
return NULL; //return NULL if it was not found
}
A correct call would be ...???
Student *s = find(NameToFind);
if(s!=NULL)
//do something....
Thx,
Tommy

HTH,
Kalyan
Jul 22 '05 #3

"c wood" <re******@verizon.net> wrote in message
news:4H****************@nwrddc01.gnilink.net...

[SNIP]
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray[i]; //Add current array value(object) to temp
This does not add the value to temp but causes the pointer temp to point to
the i'th element of MyArray.

char *name;//Create a char pointer
name = temp->get_name();//Get object name (public method of
Student)

if(name == chName)//Compare names


Means: if (char * == char *)

That would be true if both strings WERE the same, I mean:


Just to get things straight. This were true if the pointers were the same
and not the strings!

[SNIP]

Regards
Chris
Jul 22 '05 #4
Ever heard of map? You're in a C++ newsgroup, so the best advice I can
give you is to go for map.

It's easier too...

Here's an example...

#include <map>
#include <string>
#include <iostream>

using namespace std;
map<string,string> data;
void AddData(string Key, string Value)
{
data[Key] = Value;
}

void RemoveData(string Key)
{
data[Key] = "";
}

bool Search(string KeyToFind)
{
if (data[KeyToFind] != "")
return true;
return false;
}

int main()
{
...
...
//use those functions
...
...

return 0;
}

-

It's really simple. Check it out. Of course you can have whatever kind
of map you want. For example map<string,int> (empty field value is 0),
map<int,int> (same empty field value) etc. etc.

-cmad
Jul 22 '05 #5

"Chris Mantoulidis" <cm****@yahoo.com> wrote in message
news:a8**************************@posting.google.c om...
Ever heard of map? You're in a C++ newsgroup, so the best advice I can
give you is to go for map.

It's easier too...

Here's an example...

#include <map>
#include <string>
#include <iostream>

using namespace std;
map<string,string> data;
void AddData(string Key, string Value)
You might want to change the string parameters to const string& to avoid
unnecessary copies! This applies to all of these functions.
{
data[Key] = Value;
}

void RemoveData(string Key)
{
data[Key] = "";
}


I'm not sure this is very useful because what do you need the key for if
there is no data attached to it?

[SNIP]

Regards
Chris
Jul 22 '05 #6

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

5
by: amit kumar | last post by:
I am calling a function which returns pointer to a map. The declaration of the map is map<int,vectxyz*>. vectxyz is a vector containing pointer to a class xyz. For map<int,vectxyz*>* p1 In the...
10
by: Dirk Vanhaute | last post by:
I have only small knowledge of c++, but I would like to compile the example in http://support.microsoft.com/kb/q246772/ HOWTO: Retrieve and Set the Default Printer in Windows I included "#include...
8
by: Tweaxor | last post by:
Hey, I was trying to figure out was it possible in C to pass the values in an array from one function to another function. Is the possible in C? ex. y is the array that holds seven values If...
11
by: Marco Loskamp | last post by:
Dear list, I'm trying to dynamically generate functions; it seems that what I really want is beyond C itself, but I'd like to be confirmed here. In the minimal example below, I'd like to...
17
by: Razzel | last post by:
I created this as a test: #include <time.h> main(){ printf(X1: %s\n", putim()); printf(X2: %s\n", putim()); } putim() { time_t t; time(&t); return(ctime(&t));
4
by: ranjmis | last post by:
Hi all, I have come across a piece of code wherein a function returns a function pointer as it seems to me but not very clear from the prototype. As shown below - although return type is void...
17
by: I.M. !Knuth | last post by:
Hi. I'm more-or-less a C newbie. I thought I had pointers under control until I started goofing around with this: ...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
4
by: Jeffrey Spoon | last post by:
Hello, I am trying to make a simple function that returns a pointer to another function. In my header file I've declared my function pointer: void (*pStateFunction) (void); //assume the function...
11
by: Antoninus Twink | last post by:
What's the correct syntax to define a function that returns a pointer to a function? Specifically, I'd like a function that takes an int, and returns a pointer to a function that takes an int and...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.