gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 16:42:01 24 4
gpt4 key购买 nike

这两种带有结构的 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/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com