结构和类型定义 [英] struct and typedef
问题描述
在C以下相同呢?
// #1
struct myStruct {
int id;
char value;
};
typedef struct myStruct Foo;
// #2
typedef struct {
int id;
char value;
} Foo;
如果不是,哪一个我应该使用什么时候?
If not, which one should I use and when?
(是的,我已经看到这和<一个href=\"http://stackoverflow.com/questions/612328/difference-between-struct-and-typedef-struct-in-c\">this.)
(Yes, I have seen this and this.)
推荐答案
没有,他们不是完全等同。
No, they're not exactly equivalent.
在第一个版本富
是一个typedef命名结构MYSTRUCT
。
In the first version Foo
is a typedef for the named struct myStruct
.
在第二个版本,富
是的typedef
的一位不愿透露姓名结构
。
In the second version, Foo
is a typedef
for an unnamed struct
.
虽然这两个富
可以以同样的方式在很多情况下使用的有重要区别。尤其是第二个版本不允许使用前向声明来声明富
和结构
这是一个的typedef
对,而在第一部分。
Although both Foo
can be used in the same way in many instances there are important differences. In particular, the second version doesn't allow the use of a forward declaration to declare Foo
and the struct
it is a typedef
for whereas the first would.
这篇关于结构和类型定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!