c - 这两种具有结构的 typedef 实现是否等效?

标签 c struct typedef

这两种带有结构的 typedef 实现之间有什么区别(如果有的话)?

typedef struct Data{
    int x;
    int y;
} Data;

struct Data{
    int x;
    int y;
};
typedef struct Data Data;

最佳答案

是的,它们都 typedef Datastruct Data,尽管人们通常会使用不同的名称以避免混淆。

请注意,在 C++ 中,使用类型时根本不需要 struct 部分,因此通常根本没有 typedef 。这同样适用于enum等。

struct Data
{
    int x;
    int y;
};
void foo1(Data data); // OK in C++
void foo2(struct Data data); // OK in C or C++, but almost never seen in C++


typedef struct Data2_
{
    int x;
    int y;
}Data2;
void foo3(Data2 data); // OK in C or C++, but C++ code tends to skip the typedef as well

关于c - 这两种具有结构的 typedef 实现是否等效?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46057500/

相关文章:

c++ - const char myVar* 与 const char myVar[]

c++ - 错误 C2823 : a typedef template is illegal - function pointer

c++ - 没有定义的 typedef 结构

c - 构造变量后使其成为常量

c - 为什么我在 send() 函数中丢失了这个字节?

c - 在 C、visual studio 中释放内存时出错

c - "memset"设置结构导致 printf 打印结构成员的长度超过其限制

json - 在 Go 中解码动态 json 内容

c++ - 从文件中读取信息

c++ - typedef 结构到结构数组