What is the meaning of `struct X typedef` vs. `typedef struct X`?(`struct X typedef` 与 `typedef struct X` 的含义是什么?)
问题描述
我在现有代码库中有以下(工作)代码,在 C 和 C++ 之间共享的包含文件中使用,在 MSVC (2010) 和 Windows DDK 上编译:
I have the following (working) code in an existing code base, used in include file that is shared between C and C++, compiling on MSVC (2010) and Windows DDK:
struct X {
USHORT x;
} typedef X, *PX;
还有:
enum MY_ENUM {
enum_item_1,
enum_item_2
} typedef MY_ENUM;
据我所知,正确的定义应该是这样的:
As far as I know, correct definition should look like this:
typedef struct {
USHORT x;
} X, *PX;
下面的表格有什么目的吗?我错过了什么吗?
Is there any purpose for having the form below? Am I missing something?
推荐答案
typedef 和 是有效的,仅来自语言语法定义.
The fact that both typedef <type> <alias> and <type> typedef <alias> are valid simply comes from the language grammar definition.
typedef 被归类为 storage-class specfifier(就像 static、auto),并且类型本身被称为类型说明符.从标准第 6.7 节中的语法定义中,您会看到这些可以自由互换:
typedef is classified as a storage-class specfifier (just like static, auto), and the type itself is known as the type-specifier. From the syntax definitions in section 6.7 of the standard, you'll see that these are free to be interchanged:
declaration:
declaration-specifiers init-declarator-list ;
declaration-specifiers:
storage-class-specifier declaration-specifiers
type-specifier declaration-specifiers
type-qualifier declaration-specifiers
function-specifier declaration-specifiers
init-declarator-list:
init-declarator
init-declarator-list , init-declarator
init-declarator:
declarator
declarator = initializer
(当然,请注意,这对于结构体和非结构体同样适用,这意味着 double typedef 麻烦; 也是有效的.)
(Note, of course, that this is equally true for structs and for non-structs, meaning that double typedef trouble; is also valid.)
这篇关于`struct X typedef` 与 `typedef struct X` 的含义是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!
本文标题为:`struct X typedef` 与 `typedef struct X` 的含义是什么?
基础教程推荐
- 如果我为无符号变量分配负值会发生什么? 2022-01-01
- 为什么 RegOpenKeyEx() 在 Vista 64 位上返回错误代码 2021-01-01
- 为什么 typeid.name() 使用 GCC 返回奇怪的字符以及如 2022-09-16
- GDB 显示调用堆栈上函数地址的当前编译二进制文 2022-09-05
- 初始化列表*参数*评估顺序 2021-01-01
- 我应该对 C++ 中的成员变量和函数参数使用相同的名称吗? 2021-01-01
- 通过引用传递 C++ 迭代器有什么问题? 2022-01-01
- 为什么派生模板类不能访问基模板类的标识符? 2021-01-01
- CString 到 char* 2021-01-01
- 非静态 const 成员,不能使用默认赋值运算符 2022-10-09
