This statement and example is from Essential C by Nick Parlante.
One nice thing about the C type syntax is that it avoids the circular definition problems which come up when a pointer structure needs to refer to itself. The following definition defines a node in a linked list. Note that no preparatory declaration of the node pointer type is necessary.
struct node {
int data;
struct node* next;
};
How does the C compiler know what is struct node*
when it is still inside the struct node.
There is another example of circular definition, wherein a type struct treenode*
is used before it is defined further down.
typedef struct treenode* Tree;
struct treenode {
int data;
Tree smaller, larger; // equivalently, this line could say
// "struct treenode *smaller, *larger"
};
How does the C compiler know what is struct treenode*
when it hasn't been defined yet.
Related SO Question: How does C resolve circular definition when a pointer in struct points to the struct itself? (This is related, it doesn't answer the "How" question).
Edit: I am assuming that C compiler is able to do this in a single pass.