What are the differences between these two typedef styles in C?

后端 未结 6 1323
野趣味
野趣味 2021-01-11 14:24

I\'m curious what the difference here is when typedefing an enum or struct. Is there any difference semantically between these two blocks?

This:

typ         


        
6条回答
  •  感动是毒
    2021-01-11 14:33

    The first form creates an anonymous enum type and creates a SomeEnum alias to it.

    The second form creates both a enum SomeEnum type and a SomeEnum alias to it.

    (In C, there are separate namespaces for types. That is, struct Foo is different from enum Foo which is different from Foo.)

    This is more important for structs than enums since you'd need to use the second form if your struct were self-referential. For example:

    struct LinkedListNode
    {
        void* item;
        struct LinkedListNode* next;
    };
    
    typedef struct LinkedListNode LinkedListNode;
    

    The above wouldn't be possible with the first form.

提交回复
热议问题