Public Group

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

This topic is 3273 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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]

##### Share on other sites
struct Data2; // forward declarationstruct Data{    struct Data2 *data2;    int value;    float threshold;    clock_t timestamp;};struct Data2{    int num_datas;    struct Data **datas;};

##### Share on other sites
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.

##### Share on other sites
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'

##### Share on other sites
Quote:
 Original post by aziiiIt 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.

##### Share on other sites
Quote:
Original post by implicit
Quote:
 Original post by aziiiIt 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.

##### Share on other sites
Quote:
Original post by aziii
Quote:
 Original post by doynaxThat'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.

1. 1
2. 2
3. 3
4. 4
Rutin
12
5. 5

• 26
• 10
• 9
• 9
• 11
• ### Forum Statistics

• Total Topics
633694
• Total Posts
3013378
×