1

I am trying to access the values of struct members inside an array of struct pointers inside of a struct. So I have a struct Room that contains an array of pointers to other Rooms called outboundConnection[]. I keep getting the error

error: request for member ‘name’ in something not a structure or union printf("Connection %i: %s", i, x.outboundConnections[i].name);

My struct is set up like this:

typedef struct
{
    char* name; //Name of the room
    char type; //Type of room
    int numOutboundConnections; //Number of rooms connected
    int isSelected;; // 0 means not selected, 1 means selected 
    struct Room *outboundConnections[6]; //Pointers to rooms connected
} Room;
// Room constructor used for filling roomBank
Room room_init(char* name, int s, int c)
{
    Room temp;
    temp.name = calloc(16, sizeof(char));
    strcpy(temp.name, name);
    temp.isSelected = s;
    temp.numOutboundConnections = c;
    return temp;
}

I am adding connections to the outboundConnections array using this function:

void ConnectRoom(Room *x, Room *y)
{
    (*x).outboundConnections[(*x).numOutboundConnections] = malloc(sizeof(Room));
    (*x).outboundConnections[(*x).numOutboundConnections] = y;
    (*x).numOutboundConnections++;
    (*y).outboundConnections[(*y).numOutboundConnections] = malloc(sizeof(Room));
    (*y).outboundConnections[(*y).numOutboundConnections] = x;
    (*y).numOutboundConnections++;
}

I am having issues with getting the name struct member in the outboundConnections array.

printf("Connection %i: %s", i, x.outboundConnections[i].name);

I have tried using ->name and (*x.outboundConnections[i]).name. I am wondering if I am even correctly assigning Rooms to the outboundConnections array or if my issue is how I am trying to access the member variable.

3 Answers 3

1

outboundConnections is an array of pointers, so outboundConnections[i] is a Room *. Also, x is a pointer as well. You should use -> (as opposed to .) to access its members:

x->outboundConnections[i]->name
Sign up to request clarification or add additional context in comments.

3 Comments

I tried this an got another error of error: invalid type argument of ‘->’ (have ‘Room’)
where does this printf appear. In that context, what is x?
It in in a function where I was trying to print out the name member variable of the Room structs in outboundConnections. Here is the function: void testConnections(Room x) { int i; for(i=0; i<6; i++) { printf("Connection %i: %s", i, x->outboundConnections[i]->name); } }
1

First off, replace the (*x). notation with x-> as others have pointed out.

But more importantly, when you do this:

(*x).outboundConnections[(*x).numOutboundConnections] = malloc(sizeof(Room));
(*x).outboundConnections[(*x).numOutboundConnections] = y;

You just leaked sizeof(Room) bytes. The malloc call is unnecessary if you're storing pointers to Room. You would only need it if you were storing a copy of Room. So your code should look like this if you're storing pointers:

x->outboundConnections[x->numOutboundConnections] = y;

Or this if you're storing a copy of Room:

x->outboundConnections[x->numOutboundConnections] = malloc(sizeof(Room));
*x->outboundConnections[x->numOutboundConnections] = *y;

Comments

0

outboundConnections is an array of pointers, so you have to dereference the pointer to get to the name member.

printf("Connection %i: %s", i, x->outboundConnections[i]->name);

In all cases where you write (*x).something you can simplify that to x->something.

Another problem is in the declaration of the structure itself. You have:

struct Room *outboundConnections[6]; 

but you've never declared struct Room. You declared an anonymous structure and then gave it a typedef Room. You need to change the declaration to:

typedef struct Room
{
    char* name; //Name of the room
    char type; //Type of room
    int numOutboundConnections; //Number of rooms connected
    int isSelected;; // 0 means not selected, 1 means selected 
    struct Room *outboundConnections[6]; //Pointers to rooms connected
} Room;

13 Comments

I tried this an got another error of error: invalid type argument of ‘->’ (have ‘Room’)
I don't understand that. x is Room*, not Room.
You didn't do temp->, did you? temp is a structure, not a pointer.
You need to go back to your textbook or tutorial and review the sections on structures and pointers, so you know when to use . or ->.
You don't have struct Room declared anywhere.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.