What is the meaning of `struct X typedef` vs. `typedef struct X`?
The fact that both typedef <type> <alias>
and <type> typedef <alias>
are valid simply comes from the language grammar definition.
typedef
is classified as a storage-class specfifier (just like static
, auto
), and the type itself is known as the type-specifier. From the syntax definitions in section 6.7 of the standard, you'll see that these are free to be interchanged:
declaration: declaration-specifiers init-declarator-list ;declaration-specifiers: storage-class-specifier declaration-specifiers type-specifier declaration-specifiers type-qualifier declaration-specifiers function-specifier declaration-specifiersinit-declarator-list: init-declarator init-declarator-list , init-declaratorinit-declarator: declarator declarator = initializer
(Note, of course, that this is equally true for structs and for non-structs, meaning that double typedef trouble;
is also valid.)
As others said, typedef
is a storage-class specifier and as with other storage-class specifiers you are also allowed to put the specifier between the type and the declarator.
While this is valid and it is also a form that should be avoided as C marked it as an obsolescent feature:
(C11, 6.11.5p1) "The placement of a storage-class specifier other than at the beginning of the declaration specifiers in a declaration is an obsolescent feature."
Both have the same meaning. Both of these two forms are valid:
typedef <existing_type> <new_type><existing_type> typedef <new_type>
You can typedef
the above struct in either ways:
struct X { USHORT x;}typedef X, *PX; // <existing_type> typedef <new_type>
or
typedef struct { USHORT x;} X, *PX; // typedef <existing_type> <new_type>