1

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];"

  • 4
    You cannot put a structure inside of itself. If you could, the structures inside the structure would have more structures inside them, and it would require infinite space. You can put pointers to the same type of structure inside a structure. A member declared as `struct room *connections[6];` would be an array of six pointers to `struct room` objects. And a member declared as `struct room **connections` would be a pointer to a pointer to a `struct room`, which could be the first such pointer; you could dynamically allocate whatever number of them is needed for the current room. – Eric Postpischil Sep 02 '18 at 22:17
  • What you have is a recursively-allocated structure (which would take an infinite amount of storage). What you want is a structure that contains pointers which can point to other structures of its type. – Joseph Myers Sep 02 '18 at 22:17
  • I would suggest giving your rooms an integer ID and either storing a list of room IDs; or (preferably) having a data structure representing the connections that's stored entirely separate from the rooms – M.M Sep 03 '18 at 02:14

2 Answers2

1

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;
};
Carl
  • 2,057
  • 1
  • 15
  • 20
1

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.

P.W
  • 26,289
  • 6
  • 39
  • 76
  • 1
    Just a matter of taste, but generally the `'*'`should be attached to the `variable` (e.g. `struct room *connections[6];`) to disambiguate the type -- because `char* a, b, c;` certainly does not declare 3 *pointers* to `char`. – David C. Rankin Sep 03 '18 at 04:25
  • When declaring multiple pointers on the same line, it is better to have a typedef. `typedef char* charPtr; charPtr a, b, c;` – P.W Sep 03 '18 at 04:38
  • 1
    You will want to review: [Is it a good idea to **typedef** pointers?](http://stackoverflow.com/questions/750178/is-it-a-good-idea-to-typedef-pointers) – David C. Rankin Sep 03 '18 at 05:37