什么是用C这两种类型定义样式之间的区别是什么? [英] What are the differences between these two typedef styles in C?

查看:125
本文介绍了什么是用C这两种类型定义样式之间的区别是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我很好奇,这里的区别是什么typedefing枚举或结构时。有什么不同语义这两个块之间?

I'm curious what the difference here is when typedefing an enum or struct. Is there any difference semantically between these two blocks?

typedef enum { first, second, third } SomeEnum;

和这样的:

enum SomeEnum { first, second, third };
typedef enum SomeEnum SomeEnum;

同样的协议的结构。我在使用中既看到,他们似乎都在做C或Objective-C的同样的事情。是否有一个真正的区别或者是它只是一个preference为哪种风格你可以使用?

Same deal for structs. I've seen both in use, and they both seem to do the same thing in C or Objective-C. Is there a real difference or is it just a preference for which style you can use?

推荐答案

所不同的是第二种方法声明了一个名为枚举SomeEnum 类型,还声明的typedef名 SomeEnum - 该类型的别名。它实际上可以组合成等价的单行

The difference is that the second approach declares a type named enum SomeEnum and also declares a typedef-name SomeEnum - an alias for that type. It can actually be combined into the equivalent one-liner

typedef enum SomeEnum { first, second, third } SomeEnum;

这使得它相当明显,这两个方法之间的唯一区别是是否有在枚举关键字后面的名称。在第二种方法中,你可以通过使用声明枚举类型的对象,无论是 SomeEnumè枚举SomeEnumè为准您preFER。

which makes it rather obvious that the only difference between the two approaches is whether there's a name after the enum keyword. With the second approach, you can declare object of that enum type by using either SomeEnum e or enum SomeEnum e, whichever you prefer.

第一种方法只有声明的typedef名 SomeEnum 为最初的匿名枚举类型,这意味着你仅限于 SomeEnumè声明。

The first approach only declares the typedef-name SomeEnum for an originally anonymous enum type, meaning that you are limited to SomeEnum e declarations.

所以,只要你只在你的声明使用的typedef名 SomeEnum ,会出现两者之间没有什么区别。但是,在某些情况下,您可能需要使用类型枚举SomeEnum 的完整的原始名称。在第一种方法的名称不可用,所以你是出于运气。

So, as long as you only use the typedef-name SomeEnum in your declarations, there will be no difference between the two. However, in some cases you might have to use the full original name of the type enum SomeEnum. In the first approach that name is not available, so you'll be out of luck.

例如,如果上述声明之后,你还声明在某些嵌套的作用域命名为 SomeEnum 变量

For example, if after the above declaration you also declare a variable named SomeEnum in some nested scope

int SomeEnum;

该变量的名称将隐藏枚举的typedef的名称,从而使这一声明非法

the name of the variable will hide the typedef-name of the enum, thus making this declaration illegal

SomeEnum e; /* ERROR: `SomeEnum` is not a type */

然而,如果你声明你枚举时使用第二种方法,可以解决此问题通过使用完整的类型名称

However, if you used the second approach when declaring your enum, you can work around this problem by using the full type name

enum SomeEnum e; /* OK */

如果声明的枚举类型时所使用的第一种方法这将是不可能的。

This would not be possible if you used the first approach when declaring your enum type.

在与结构使用的名称后面结构是必须的,当你需要一个自引用类型(即包含一个指向同一类型的类型)像

When used with structs, the name after the struct is a must when you need a self-referencing type (a type that contains a pointer to the same type), like

typedef struct SomeStruct {
  struct SomeStruct *next;
} SomeStruct;

最后,在第二个方法的typedef名称完全是可选的。你可以简单地声明

Finally, in the second approach the typedef name is totally optional. You can simply declare

enum SomeEnum { first, second, third };

和只使用枚举SomeEnum 每次你需要参考这一类的时间。

and just use enum SomeEnum every time you need to refer to this type.

这篇关于什么是用C这两种类型定义样式之间的区别是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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