Code without typedef
(and it works):
struct Node {
int data;
struct Node *next;
struct Node *prev;
};
I'm trying to make a code using typedef
for the structure "Node" in Doubly Linked List, but this does not work:
typedef struct {
int data;
Node *next;
Node *prev;
} Node;
Is there a way around this using typedef
?
Inside the typedef, the to-be-defined type is not yet known, so you need to introduce and use a struct tag:
typedef struct Node_tag {
int data;
struct Node_tag *next;
struct Node_tag *prev;
} Node;
The Node_tag
is the struct tag, because of where it is introduced (and not because of the name part "_tag"). It is not a type in itself, only the combination as struct Node_tag
is a type which can be used for the struct members.
Afterwards, when the typedef is done, the type Node
has been defined.
To clarify, a second typedef would be possible as typedef struct Node_tag NodeToo;
. This demonstrates that the type struct Node_tag
is also useable. That is why I prefer to use the "_tag" name fragment, to allow to be clear of what is used.