Is typedef ever required in C?

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

    Yes. Integer types that have to be a fixed size. e.g. int32_t (and you want your code to have a fighting chance of being portable).

    0 讨论(0)
  • 2021-02-13 15:13

    No. I think I'm safe in saying that!

    0 讨论(0)
  • 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.

    0 讨论(0)
提交回复
热议问题