Is it impossible to do something like this in C?
struct room{
//Name of the room
char* name;
//Type fo the room
char* type;
//Array of outbound connections, max of six
struct room connections[6];
//A counter variable for how many connections the room actually has been assigned
int numOfConnections;
};
I am creating a map of rooms which are connected to each other, and I thought the easiest way for each room to keep track of the rooms it is connected to would be to make an array of room structs and then put the rooms in their.
I am getting an error that says room the array has an incomplete element type. The error is on the line "struct room connections[6];"
In order to store a struct
inside itself, it must be of pointer type. Otherwise, as mentioned in comments, this struct
would take infinite space. The change below makes it into a pointer to 6 struct room
's.
struct room{
//Name of the room
char* name;
//Type fo the room
char* type;
//Array of outbound connections, max of six
struct room* connections[6];
//A counter variable for how many connections the room actually has been assigned
int numOfConnections;
};
I am creating a map of rooms which are connected to each other
The solution you have chosen (of having an array of rooms
within a structure of room
) does not represent your problem. That is like having other rooms
within a room
. And this is not possible to do either as your error message shows.
What you need to store within a structure of room
are the links (or addresses) of the other rooms
it is connected to. Doing this is possible as it is a very well defined problem with a clear solution. So in the struct room
you store the pointers (which are addresses) to the rooms it is connected to.
struct room* connections[6];
The above line of code means that connections
is an array of 6 elements which are pointers to the struct room
.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.