A function that returns a pointer...??

T

Tommy Lang

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; //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
 
C

c wood

Tommy Lang said:
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; //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.
 
K

Kalyan

(e-mail address removed) (Tommy Lang) wrote in message
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; //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
 
C

Chris Theis

c wood said:
[SNIP]
{
//Check whole array
for(int i=0;i<100;i++)
{
Student *temp;//Create a temp pointer
temp = MyArray; //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.
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
 
C

Chris Mantoulidis

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
 
C

Chris Theis

Chris Mantoulidis said:
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
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top