Thread: Return a pointer to a struct that is in an array of structs?

  1. #1
    Registered User
    Join Date
    May 2017
    Posts
    6

    Return a pointer to a struct that is in an array of structs?

    I have a struct:
    Code:
    struct room
    {
    char name[32]; char type[32]; int numConnections;
    }
    I have an array of these structs:
    Code:
    struct room rooms[7];
    I am trying to create a function that returns a pointer to the struct that has the same name as what was passed into the function.

    Code:
    struct room *getRoomByName(const char* name)
    {
    struct room *current; for (int i = 0; i < 7; i++)
    {
    if ((strcmp(rooms[i].name, name) == 0)) {
    *current = rooms[i];
    }
    } return current;
    }
    *accidently hardcoded the name in that strcmp()

    I pretty much have no idea what I'm doing.
    Last edited by potatoes1; 05-10-2017 at 05:55 PM.

  2. #2
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    28,413
    Your basic loop idea looks alright, but you need to pass the array of rooms and also avoid hardcoding the size, e.g.,
    Code:
    struct room *getRoomByName(struct room rooms[], size_t num_rooms, const char* name)
    {
        struct room *current;
        for (size_t i = 0; i < num_rooms; i++)
        {
            if (strcmp(rooms[i].name, "roomname") == 0)
            {
                *current = rooms[i];
            }
        }
        return current;
    }
    Of course, the above is still not correct:
    • You have the name parameter, so you should use that instead of hardcoding the room name.
    • current is a pointer; it does not point to any object. Therefore, you cannot dereference it with *current. But that's not a problem because you want current to point to rooms[i], i.e., assign the address of rooms[i] to current.
    • What happens if no room with the given room name is found? It would be good to initialise current to be a null pointer so that the caller can check for a null pointer and hence determine if there was a room with the given room name.

    Although unlikely to be a matter of correctness, if you assume that the room names are unique, or if you want the first matching room, then you should break after assigning to current.
    Quote Originally Posted by Bjarne Stroustrup (2000-10-14)
    I get maybe two dozen requests for help with some sort of programming or design problem every day. Most have more sense than to send me hundreds of lines of code. If they do, I ask them to find the smallest example that exhibits the problem and send me that. Mostly, they then find the error themselves. "Finding the smallest program that demonstrates the error" is a powerful debugging tool.
    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  3. #3
    Registered User
    Join Date
    May 2017
    Posts
    6
    Made the changes you mentioned and it works now. The pointer thing is what was tripping me up. Thank you very much!

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Malloc a struct pointer inside an array of structs
    By arortell in forum C Programming
    Replies: 10
    Last Post: 10-22-2014, 03:50 PM
  2. Return an array of structs.... What am I missng.
    By shick in forum C++ Programming
    Replies: 10
    Last Post: 03-31-2014, 12:07 PM
  3. Replies: 4
    Last Post: 09-14-2012, 01:51 PM
  4. return an array of structs
    By MK27 in forum C Programming
    Replies: 14
    Last Post: 02-14-2010, 08:31 PM
  5. Weird struct pointer return
    By Devil Panther in forum C Programming
    Replies: 2
    Last Post: 07-24-2005, 12:05 PM

Tags for this Thread