c - 带有 typedef 的结构

标签 c struct typedef

<分区>

Possible Duplicate:
Purpose of struct, typedef struct, in C++
typedef struct vs struct definitions

我知道在 C 中有两种声明结构的方法

struct point{
    int x, y;
};

和:

typedef struct{
    int x, y;
} point;

但是这两种方法有什么区别,什么时候我应该使用 typedef 方法而不是其他方法?

最佳答案

区别:

  1. 第一个是有效的C89;
  2. 第一个没有使结构的标签(“point”)成为一个完整的类型定义,因此您可以仅将 point p; 与第二个一起使用。
  3. 我们的 C 编程之神 Linus Torvalds 强烈建议不要对结构使用 typedef(除非明确需要将实际字段和类型隐藏在不透明结构后面),因为它会降低可读性。<

关于c - 带有 typedef 的结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12128922/

相关文章:

c - 在 C 中破坏结构的最佳方法是什么

C# : this() mean after a constructor of struct 是什么

c - 使用堆将元素插入优先级队列

c - 在 C 数组中打印非重复项

c - 释放指针重新分配自身?

c - 如何理解以下有关 C 中 typedef 的代码

c++ - 如何定义一个可以保存 uintptr_t 或 uint32_t 而无需 union 的类型?

c 将 typedef 放入结构中

javascript - 从客户端接收消息并将消息发送到 Node js服务器

包含不同 header 时,C 实现是否可以隐式包含标准 header ?