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

前端 未结 5 1948
走了就别回头了
走了就别回头了 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 13:40

    The fact that both typedef and typedef 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-specifiers
    
    init-declarator-list:
        init-declarator
        init-declarator-list , init-declarator
    
    init-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.)

提交回复
热议问题