Given the following code:
typedef struct elementT {
int data;
struct elementT *next;
} element;
Why is it necessary to do struct elementT *next
and I can't do element *next
inside the struct declaration itself? Is it because it is not declared yet?
The
typedef
only takes place after thestruct
has been defined. Consider the following code; the syntax is invalid, but it hopes it shows the order/precedence of things:I.e., the
struct{...}
is an "expression" that denotes a type. Thetypedef
operates on that type by giving it a name.By contrast,
works because of two special rules of C:
struct <name>
not only defines a type, but gives it astruct
tag as well, and that tag is immediately visible inside the rest of thestruct
declaration (for the obvious reason: linked lists, trees, etc. would be very painful to implement without these rules).Because C said so:
The name
elementT
, part of the type namestruct elementT
, becomes visible (as an incomplete type) as soon as the compiler sees the two tokensstruct elementT
, so you can use it as a type name inside the struct definition.The name
element
, which is a typedef name, doesn't become visible until the identifierelement
appears, which is after the end (the closing}
) of the struct definition; thus you can't use it inside the struct definition, simply because it doesn't exist yet.My own personal preference (and plenty of very smart people differ with me on this point) is not to use
typedef
at all. The type already has a perfectly good name,struct ElementT
; why add a second one? I'd just write:and refer to the type as
struct element
.If you feel that having a single-word name for the type is sufficiently useful, of course, you can still use the typedef. Just remember that the typedef name won't be visible until after the end of the declaration. And there's no need to use two different identifiers:
Now you can refer to the type either as
struct element
or aselement
.(Note that C++ has different rules; it effectively creates an implicit typedef for a
struct
(orunion
,class
, orenum
) type. In C++, thetypedef
is unnecessary but harmless.)You can achieve this behaviour if you separate the struct declaration from its definition: