By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
446,389 Members | 1,881 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 446,389 IT Pros & Developers. It's quick & easy.

A function that returns a pointer...??

P: n/a
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
Share this Question
Share on Google+
5 Replies


P: n/a

"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

P: n/a
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

P: n/a

"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

P: n/a
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

P: n/a

"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 discussion thread is closed

Replies have been disabled for this discussion.