Is typedef ever required in C?

前端 未结 15 2739
夕颜
夕颜 2021-02-13 14:19

Typedef is very useful for portable names, tag names (typedef struct foo Foo;) and keeping complicated (function) declarations readable (typedef int (*cmpfunc

15条回答
  •  有刺的猬
    2021-02-13 15:08

    The offsetof() macro requires a structname and a membername, and thus cannot be used with inline struct definitions. Well, maybe on your implementation it can, but it isn't guaranteed by the C standard.

    Update: As pointed out in the comments, this is possible:

    struct blah { int a; int b; };
    x = offsetof(struct blah, a); // legal
    

    But inlining the struct definition is not:

    x = offsetof(struct {int a; int b;}, a); // illegal
    

    The former does not contain the keyword typedef, but inlining the struct definition is not possible either. Which version is indicated by "simple writing out the derived type" is unclear.

提交回复
热议问题