-1

I have a struct:

 struct room;
 ...
 /*some more structs */

 typedef struct {     
   int state;
   int id;
   struct room* north;
   struct room* south;
   struct room* east;
   struct room* west;
   creature* creatures[10];
 } room;

And a function that prints the contents:

 void printContentsOfRoom(room* r) {
   printf("\nRoom %d:\n", r->id);
   printf("\tState: %s\n", getState(r));
   printf("\n\tNeighbors:\n");
   if (r->north.id != -1)
     printf("\t    North: Room %d\n",((room*)r->north)->id);
   if (r->east.id != -1)
     printf("\t    East: Room %d\n",((room*)r->east)->id);
   if (r->south.id != -1)
     printf("\t    South: Room %d\n",((room*)r->south)->id);
   if (r->west.id != -1)
     printf("\t    West: Room %d\n",((room*)r->west)->id);
   printf("\n\tCreatures:\n");
   for (int i = 0; i < 10; i++) {
     creature* c = (creature*)r->creatures[i];
     if (c) {
       if (c == pc)
     printf("\t    %s\n",getType(c));
       else
     printf("\t    %s %d\n",getType(c),c->id);
     }
   }
 }

I'm trying to make it so that if the room id is set to -1 (set while the program runs), it does not print the information about that room. On compilation, I get the error "request for member 'id' in something not a structure or union."

Furthermore, I have tried setting the if condition r->north->id, and that returns the error "dereferencing pointer to incomplete type 'struct room'"

1 Answers1

1

I think that you want to do a forward declaration like this:

typedef struct room room;

struct room{
    .
    .
    .
};

Then you can use r->north->id. See this SO question: How to define a typedef struct containing pointers to itself?

Community
  • 1
  • 1
ad absurdum
  • 19,498
  • 5
  • 37
  • 60