What is the meaning of `struct X typedef` vs. `typedef struct X`?

前端 未结 5 1946
走了就别回头了
走了就别回头了 2020-12-03 13:03

I have the following (working) code in an existing code base, used in include file that is shared between C and C++, compiling on MSVC (2010) and Windows DDK:



        
5条回答
  •  心在旅途
    2020-12-03 14:01

    You really are allowed to put all the declaration specifiers in any order you want! The positions of any * pointers and the actual declarator (the variable or new type name) matter, but all that typedef int unsigned const static etc. stuff can be in any order.

    If you look at the official grammar of C, it just says:

    declaration:
        declaration-specifiers init-declarator-list ;
    

    The declaration-specifiers are all the storage class specifiers (typedef, extern, etc.), type specifiers (the actual type, like int or struct X), type qualifiers (const and volatile), and a few other less common ones. Their order is not important. The second part is the init-declarator-list, and it's the variable or new type name (in the case of a typedef), any * characters, the initialization of the variable (int x = 3), and more. The order of things in the declarator part is important, but not the order in the declaration specifiers.

提交回复
热议问题