Is typedef ever required in C?

前端 未结 15 2878
夕颜
夕颜 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:15

    Thanks all for your answers. I looked some more myself and found this in C99, J.2 Undefined behaviour:

    The behavior is undefined in the following circumstances: [...]

    • The type parameter to the va_arg macro is not such that a pointer to an object of that type can be obtained simply by postfixing a * (7.15.1.1).

    So when you want to pass/extract complicated derived types to va_arg such as int (*)[] you need to typedef this type to something for which this is possible (updated/corrected):

    typedef int (*intarrptr)[4];
    intarrptr x = va_arg(ap, intarrptr);
    

    Since it is difficult to find and actual useful case for this one might conclude that it is not a strong argument for the necessity of typedef.

提交回复
热议问题