Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C++ > A function that returns a pointer...??

Reply
Thread Tools

A function that returns a pointer...??

 
 
Tommy Lang
Guest
Posts: n/a
 
      12-10-2003
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
 
Reply With Quote
 
 
 
 
c wood
Guest
Posts: n/a
 
      12-10-2003

"Tommy Lang" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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



 
Reply With Quote
 
 
 
 
Kalyan
Guest
Posts: n/a
 
      12-11-2003
http://www.velocityreviews.com/forums/(E-Mail Removed) (Tommy Lang) wrote in message news:<(E-Mail Removed). 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
 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      12-11-2003

"c wood" <(E-Mail Removed)> wrote in message
news:4HNBb.2232$(E-Mail Removed)...
>

[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


 
Reply With Quote
 
Chris Mantoulidis
Guest
Posts: n/a
 
      12-11-2003
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
 
Reply With Quote
 
Chris Theis
Guest
Posts: n/a
 
      12-11-2003

"Chris Mantoulidis" <(E-Mail Removed)> wrote in message
news:(E-Mail Removed) 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


 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
problem in running a basic code in python 3.3.0 that includes HTML file Satabdi Mukherjee Python 1 04-04-2013 07:48 PM
Sorted Returns List and Reversed Returns Iterator ++imanshu Python 7 08-23-2008 04:25 AM
Hash#select returns an array but Hash#reject returns a hash... Srijayanth Sridhar Ruby 19 07-02-2008 12:49 PM
createImage sometime returns null and sometime returns non-null. vizlab Java 3 10-17-2007 11:21 AM
block returns and hash element returns Trans Ruby 2 11-06-2005 12:15 PM



Advertisments