is this:
the same as this:Code:typedef struct node node_t; struct node { data_t data; node_t *next; };
....?Code:typedef struct node { data_t value; node_t *next; } node_t;
is this:
the same as this:Code:typedef struct node node_t; struct node { data_t data; node_t *next; };
....?Code:typedef struct node { data_t value; node_t *next; } node_t;
More or less, yes.
If you're defining a struct, I believe you have to refer to it by its formal name, however. Meaning that you can't write node_t *next, but would rather have to write it as struct node *next.
Yes, sorry, I meant actually inside the struct itself when you define it. Outside of the actual struct definition, you can do whatever you want with typedefs.
I've always been curious, is it possible for an anonymous structure to reference itself (i.e. while typedefing it)? I've always found it redundant to give a structure a name that I intend to typedef, and thus never use the structure name ever again. Something along the lines such as this:
At first I thought to just redefine the anonymous struct again while specifying the type of its the pointer ptrAnon, i.e:Code:typedef struct { (what to put here?) *ptrAnon; } anon;
but then that gets funny fast:Code:typedef struct { struct{...} *ptrAnon; } anon;
Now this may very well be the most redundantly stupid question ever, but is it absolutely necessary to give a structure a name if you intend to reference it within itself, or in other words, can an anonymous structure contain a reference to itself?Code:typedef struct { struct{struct{struct{... ad infinitum} *ptrAnon} *ptrAnon} *ptrAnon; } anon;
Why wouldn't you name your structure though?
Theres no harm from doing so...
The above works with LCC.Code:typedef struct { struct node_t *next; } node_t;
Its just all moot really, sort of like anonymous classes in java, you could name them.. but why bother coming up with a unique name if you never intend to use it again.
Cool, thanks, I'll have to try that out in gcc now.
..
And it works! Awesome!
Finally no more weird internal names for structures I want to typedef!
Now I wonder how portable it is... though I'm guessing if its supported in GCC and LCC it's probably standard.
Depends what flags you used to compile it, why not read the C89 standard regarding structures?