With your combined structure definition and typedef
, the name NODE
is not yet defined at the location when you try to use it.
typedef struct{
int data;
NODE *next; // name NODE used here
}NODE; // name NODE defined here
You can do the typedef before the structure definition to use it for a pointer.
typedef struct n NODE; // NODE defined as incomplete type
struct n {
int data;
NODE *next; // use as pointer is OK
};
// from here NODE is completely defined
But you cannot define a variable or structure field of type NODE
before the structure is really defined.
typedef struct n NODE;
#if 0
// does not work because NODE is an incomplete type here
struct wrapper {
NODE node;
};
#endif
// using a pointer to an incomplete type is OK
struct n {
int data;
NODE *next;
};
// works
struct wrapper {
NODE node;
};
You can use
typedef struct n{ // type "struct n" known here
int data;
struct n *next; // use known type
}NODE; // type alias NODE known here
because the type struct n
is already known when you use it for the pointer.
Note that in C you have to use struct n *next
(in contrast to C++).