是的typedef曾经在C语言所需? [英] Is typedef ever required in C?

查看:109
本文介绍了是的typedef曾经在C语言所需?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

类型定义是便携式的名称,标签名称非常有用的( typedef结构富富; )和
保持复杂的(功能)声明可读(的typedef INT
(* cmpfunc)(常量无效*,常量无效*);

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

但有确需真正需要的typedef用C的情况?在这里你不能用简单的写出来的派生类型完成相同的。

But are there situations in C where a typedef is really truly needed? Where you cannot accomplish the same by simple writing out the derived type.

要澄清一点:我的意思是语言的用户,而不是执行者。 stdint.h 全是第二类的一个很好的例子。

To clarify a bit: I mean for language users, not implementers. The whole of stdint.h is a good example of the second category.

感谢您的输入。我想我可以总结一下如下:

Thanks for all your input. I think I can summarise it as:


  • 的C99库需要的typedef来实现各种(U)intN_t 类型。

  • 在C89你真的想使用typedef自己创建类似便携式类型。

  • 您使用的va_arg 宏时可能需要的typedef,但我怀疑你会在实践中遇到的这些衍生类型。

  • The C99 library needs typedef to implement the various (u)intN_t types.
  • On C89 you really want to use typedefs yourself to create similar portable types.
  • You might need typedef when using the va_arg macro, but I doubt you will encounter these derivative types in practise.

推荐答案

A 的typedef ,顾名思义,一个别名。因此,你总是可以取代与实际类型的别名。它不会是一个别名,否则。

A typedef is, by definition, an alias. As such, you always could replace the alias with the actual type. It wouldn't be an alias otherwise.

这并不意味着避免的typedef 将是一个不错的主意。

That doesn't mean avoiding typedef would be a good idea.

这篇关于是的typedef曾经在C语言所需?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆