【发布时间】:2015-04-15 12:03:30
【问题描述】:
#include <stdio.h>
struct s {int;};
int main()
{
printf("Size of 'struct s': %i\n", sizeof(struct s));
return 0;
}
Microsoft C 编译器 (cl.exe) 不想编译此代码。
error C2208: 'int' : no members defined using this type
GNU C 编译器 (gcc -std=c99) 编译此代码...
warning: declaration does not declare anything
...并显示结果:
Size of 'struct s': 0
这意味着 gcc 中的struct s 是完整类型,不能重新定义。
这是否意味着完整类型的大小可以为零?
另外,如果这个声明声明了完整的结构,那么消息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;
}
【问题讨论】:
-
我认为是gcc的特性,而不是c99的特性
-
@VolAnd。谢谢。这是因为对我来说,语言规范中的
undefined behavior一词是无法理解的。运行时未定义的行为或编译时未定义的行为。 -
“警告:声明没有声明任何东西”是关于编译时间的。未定义的行为与运行时有关。
-
@VolAnd Er,不。未定义的行为只是意味着标准没有强加任何要求。
-
我同意,不可能为不正确的语言使用编写要求。但就我个人而言,“未定义的行为”是关于编写为“糟糕的编程示例”的程序,而不是关于使用“糟糕的编程示例”做任何事情的编译器
标签: c visual-c++ gcc struct c99