I\'m reading \'The C Programming Language\' and encountered a problem about typedef of struct. The code is like this:
typedef struct tnode *
You can't use a type before it is defined.
With the typedef struct tnode { ... } Treenode;
declaration, the type Treenode
is not defined until the semi-colon is reached.
The situation with typedef struct tnode *Treeptr;
is different. This tells the compiler 'there is a structure type called struct tnode
, and the type Treeptr
is an alias for a pointer to a struct tnode
'. At the end of that declaration, struct tnode
is an incomplete type. You can create pointers to incomplete types but you cannot create variables of the incomplete type (so you could define Treeptr ptr1;
or struct tnode *ptr2;
and they are the same type, but you could not define struct tnode node;
).
The body of the struct tnode
could be written as:
typedef struct tnode
{
char *word;
int count;
Treeptr left;
Treeptr right;
} Treenode;
because Treeptr
is a known alias for the type struct tnode *
before the structure is defined. You can't use Treenode *left;
because Treenode
is not a known alias until the final semi-colon is reached (roughly speaking).