A function that returns a pointer...??

Discussion in 'C++' started by Tommy Lang, Dec 10, 2003.

  1. Tommy Lang

    Tommy Lang Guest

    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
     
    Tommy Lang, Dec 10, 2003
    #1
    1. Advertising

  2. Tommy Lang

    c wood Guest

    "Tommy Lang" <> wrote in message
    news:...
    > 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.


    > {
    > 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 wood, Dec 10, 2003
    #2
    1. Advertising

  3. Tommy Lang

    Kalyan Guest

    (Tommy Lang) wrote in message news:<>...

    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
     
    Kalyan, Dec 11, 2003
    #3
  4. Tommy Lang

    Chris Theis Guest

    "c wood" <> wrote in message
    news:4HNBb.2232$...
    >

    [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.

    > >
    > > 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
     
    Chris Theis, Dec 11, 2003
    #4
  5. 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
     
    Chris Mantoulidis, Dec 11, 2003
    #5
  6. Tommy Lang

    Chris Theis Guest

    "Chris Mantoulidis" <> wrote in message
    news:...
    > 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
     
    Chris Theis, Dec 11, 2003
    #6
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. glen stark
    Replies:
    2
    Views:
    725
    Ron Natalie
    Oct 10, 2003
  2. Boris Sargos
    Replies:
    3
    Views:
    398
    John Harrison
    Apr 23, 2004
  3. Fraser Ross
    Replies:
    4
    Views:
    1,078
    Fraser Ross
    Aug 14, 2004
  4. murgan
    Replies:
    6
    Views:
    4,931
    Thad Smith
    Dec 21, 2005
  5. sqweek
    Replies:
    2
    Views:
    425
    Charles Rapp
    Jan 13, 2006
Loading...

Share This Page