[SOLVED] How can 2 structs have pointers to each others in C?

Started by
5 comments, last by implicit 14 years, 3 months ago
I could use void pointers but then I had to write type conversions always when I use the pointers. Data2 should be defined before Data, which should be defined before Data2.

struct Data
{
    struct Data2 *data2;
    int value;
    float threshold;
    clock_t timestamp;
};

struct Data2
{
    int num_datas;
    struct Data **datas;
};




[Edited by - aziii on December 27, 2009 10:59:51 AM]
Advertisement
struct Data2; // forward declarationstruct Data{    struct Data2 *data2;    int value;    float threshold;    clock_t timestamp;};struct Data2{    int num_datas;    struct Data **datas;};
Have you tried compiling it, and if so which error message did you get? Your code should work as-is as far as I can see.
I didn't know you can have forward declarations for structs as well. Thanks!

It doesn't compile without forward declaration if you try compiling it (I used gcc):
src/game/particles.h:33: error: expected specifier-qualifier-list before 'ParticleSystem'

Quote:Original post by aziii
It doesn't compile without forward declaration if you try compiling it (I used gcc)
That's odd, your example code compiles just fine in my version of GCC.
Perhaps you left out the explicit struct specifier before the pointer definition? If so then a forward declaration might help, though only in C++ rather than in plain C.
Quote:Original post by implicit
Quote:Original post by aziii
It doesn't compile without forward declaration if you try compiling it (I used gcc)
That's odd, your example code compiles just fine in my version of GCC.
Perhaps you left out the explicit struct specifier before the pointer definition? If so then a forward declaration might help, though only in C++ rather than in plain C.


This is the actual code (without polluting the forum with 200 lines of real code):

typedef struct Data2 Data2;typedef struct Data{    Data2 *pointer;} Data;struct Data2{    Data *pointer;};


No keywork "struct" in previous error message, as you can see. Sorry for inconvenience.
Quote:Original post by aziii
Quote:Original post by doynax
That's odd, your example code compiles just fine in my version of GCC.
Perhaps you left out the explicit struct specifier before the pointer definition? If so then a forward declaration might help, though only in C++ rather than in plain C.

No keywork "struct" in previous error message, as you can see. Sorry for inconvenience.

Sorry, that didn't quite come out right. You're probably better off leaving the "structs" out anyway. The point is that the compiler needs to somehow infer which type of object it's dealing with when defining a pointer since certain types of pointers might be represented in quite different ways internally (e.g. function pointers.)

In the old days, and even today among certain C coders, the struct/union was explicitly included so the compiler could always figure it out. However in C++ some typing has been spared (at the expense of clarity some might argue) by automatically including structures in the global namespace, much as if a typedef on the structure had been used in C, hence the need for the forward declarations to inform the compiler of what's going on (curiously an isolated forward declaration of this kind in C is syntactically valid but essentially meaningless.)

The new C++ way is actually better than the old C way aside from reducing typing. In C it is common to define a structure directly through a typedef while leaving the the actual structure name (which coincidentally resides in an entirely separate namespace) blank, thus creating a typedef for an anonymous structure and leading to frustrating situations where it is impossible to define a pointer to a particular structure (FILE * springs to mind) without first including its header.

This topic is closed to new replies.

Advertisement