C - Accessing a structure within a structure

I was doing a project in C. And I am pretty new to C. In a project I have a structure like this:

struct room_t{
   char* name;
   struct room_t* north;
   struct room_t* east;
   struct room_t* south;
   struct room_t* west;
}

      

declared as: struct room_t room[3]

If I want to access the title in the north, I do this: room[0].north[0]->name

I'm right? or should it beroom[0].north[0].name

+3


source to share


2 answers


Your first guess is almost correct, but it should be room[0].north->name

since you didn't declare an array for the variable north

.



In general, you should try and then see what the compiler and your program output to find out as much as possible.;)

+6


source


It should be room [0] .north-> name if you allocate one element to the north But if you allocate an array (more than one element) to the north you should access as room [0] .north [n] .name where n = 0 to (number of selected elements) - 1



+4


source







All Articles