结构和类型定义 [英] struct and typedef

查看:102
本文介绍了结构和类型定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在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屋!

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