4

I'm using GBDK C to create a Game for the original Game Boy, and I have run in to a little problem. Each room in my game needs to have different portals, but each portal needs to reference a room. Here is a cut-back version of the code:

typedef struct {
    Portal portals[10];
} Room;

typedef struct {
    Room *destinationRoom;
} Portal;

Any suggestions on how to achieve this? I tried adding a forward declaration of struct Portal; to the top of the file but it didn't help.


Using the following code:

typedef struct Room Room;
typedef struct Portal Portal;

struct Room {
    Portal portals[10];
};

struct Portal {
    Room *destinationRoom;
};

Gives me this error:

parse error: token -> 'Room' ; column 11
*** Error in `/opt/gbdk/bin/sdcc': munmap_chunk(): invalid pointer: 0xbfe3b651 ***
Skatox
  • 4,237
  • 12
  • 42
  • 47
bashaus
  • 1,614
  • 1
  • 17
  • 33

1 Answers1

5

Reorder the definitions and write a forward declaration for the Room and Portal types:

typedef struct Room Room;
typedef struct Portal Portal;

struct Portal {
    Room *destinationRoom;
};

struct Room {
    Portal portals[10];
};

Note that I separated the typedef Portal from the actual struct Portal definition for consistency, even though it is not strictly necessary.

Also note that this style is compatible with C++, where the typedef is implicit but can be written explicitly this way, or with a simple forward declaration like struct Room;

If for some reason you cannot use the same identifier for the struct tag and the typedef, you should declare the structures this way:

typedef struct Room_s Room;
typedef struct Portal_s Portal;

struct Portal_s {
    Room *destinationRoom;
};

struct Room_s {
    Portal portals[10];
};
chqrlie
  • 131,814
  • 10
  • 121
  • 189
  • Unfortunately this did not solve my problem (see edits) – bashaus Oct 11 '15 at 07:29
  • 1
    @bashaus: You did not reorder the definitions. `struct Portal` must be defined first so its size is known when you define `struct Room`. – chqrlie Oct 11 '15 at 10:43
  • GBDK C required my struct and typedef to have different names too, so my `struct RoomStruct` has `typedef struct RoomStruct Room ` – bashaus Oct 11 '15 at 17:25
  • It's unfortunate that GBDK C has such requirements, the C language keeps separate namespaces for `struct` tags and `typedef` and variable names. GBDK C is no longer maintained, so you have to live with that. Does the code compile now? – chqrlie Oct 11 '15 at 20:02
  • Yes and no - the typedefs and structs are no longer a problem (which means the question is answered). The next issue I have been having is about literal declarations of structs inside structs. I decided to split this out so that each portal object is created first and then the room object is created. Thanks for asking :) – bashaus Oct 11 '15 at 22:43
  • @bashaus: That's surprising, you should only get this kind of error if you declare a new `struct` tag inside a structure definition. Separating the `typedef`s should not have this effect. Make sure you use the same tag in the `typedef` and in the actual `struct` definition, as shown in my updated answer. – chqrlie Oct 12 '15 at 03:34