Two structs with pointers to structs of eachother's type as members?

Viewed 41

For example,

typedef struct { char * name; component ** components; } object;

typedef struct { vector3 pos, orient; object * obj; } component;

I've already tried as is and, predictably, I get an error that the first one contains a pointer to a pointer of a "component", which the compiler doesn't recognise yet. Is there a way around this?

1 Answers

You need a forward declaration of the second struct in order to satisfy the pointer declaration in the first:

struct component; // This says there is a struct named component somewhere in the code

typedef struct { char * name; struct component ** components; } object;

struct component { vector3 pos, orient; object * obj; };
Related