mez*_*oni 6 c gcc struct c99 visual-c++
#include <stdio.h>
struct s {int;};
int main()
{
printf("Size of 'struct s': %i\n", sizeof(struct s));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
Microsoft C编译器(cl.exe)不希望编译此代码.
error C2208: 'int' : no members defined using this type
Run Code Online (Sandbox Code Playgroud)
GNU C编译器(gcc -std = c99)编译此代码...
warning: declaration does not declare anything
Run Code Online (Sandbox Code Playgroud)
...并显示结果:
Size of 'struct s': 0
Run Code Online (Sandbox Code Playgroud)
这意味着struct s
在gcc中是完整类型而无法重新定义.
这是否意味着完整类型的大小可以为零?
此外,declaration does not declare anything
如果此声明声明完整的结构,该消息是什么意思?
以下是struct s
(gcc -std = c99)中完整类型的证明.
#include <stdio.h>
struct s {int;};
struct S {
struct s s; // <=========== No problem to use it
};
int main()
{
printf("Size of 'struct s': %i\n", sizeof(struct s));
return 0;
}
Run Code Online (Sandbox Code Playgroud)
根据C标准,行为未定义.
在以下情况下,行为未定义:
....
- 定义的结构或联合没有任何命名成员(包括通过匿名结构和联合间接指定的成员)(6.7.2.1).
struct s {int;};
相当于struct s {};
(没有成员),GCC允许将其作为扩展名.
Run Code Online (Sandbox Code Playgroud)struct empty { };
结构的大小为零.
这使得上述程序成为特定的编译器.