I have the following struct defined with the typedef keyword:
typedef struct{
int data;
NODE *next;
}NODE;
It gives me the following error when compiling:
error: unknown type name ‘NODE’ I looked it up, and found this stack overflow post, which said I should change it to
typedef struct n{
int data;
n *next;
}NODE;
I have two question about this.
First, what is the n
? I thought NODE
was the name of the struct, so is that a second name?
The second is, why can I put the n
as a data type, but not NODE
?
NODE
is not the name of the struct. It's a typedef
, i.e. an alias for the struct. In the first case, the struct is unnamed. In the second case, the struct is named struct n
.
The reason you can't use NODE
inside of the struct is because that name hasn't been defined yet at the time it is used. So you need to use the actual name of the struct (which is defined) in order to create a pointer to it.
Also note that the struct
keyword is required in C when using the struct's name, i.e.:
struct n *next;