"BartC" <> writes:
[...]
> For example, I had trouble the other night with creating a struct with
> self-referential members. I tried loads of combinations including forward
> declarations and got nowhere. I was about to post here, then came across
> this form in some code that was lying around:
>
> typedef struct snode{
> int value;
> int opcode;
> struct snode *aref,*bref;
> } node;
>
> That worked, but how can I turn the 'struct snode*' into a much tidier 'node
> *'? This doesn't work:
>
> typedef struct {
> int value;
> int opcode;
> node *aref,*bref;
> } node;
My suggestion is just to drop the "typedef" altogether, and refer to the
type as "struct snode":
struct snode {
int value;
int opcode;
struct snode *aref;
struct snode *bref;
};
If you feel the need to have a one-word name for the type, you can
typedef the incomplete struct type and then complete it by defining it.
Note that there's no need to use different names for the struct tag and
the typedef:
typedef struct snode snode;
struct snode {
int opcode;
snode *aref;
snode *bref;
};
Another common approach is to combine the typedef and struct
declarations -- but then the typedef isn't visible until the end
of the struct declaration:
typedef struct snode {
int opcode;
struct snode *aref;
struct snode *bref;
} snode;
But really, the type already has a perfectly good name, "struct snode".
Hiding its "structness" behind a typedef doesn't buy you anything.
--
Keith Thompson (The_Other_Keith)
kst- <http://www.ghoti.net/~kst>
Will write code for food.
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"