【发布时间】:2011-09-12 04:57:31
【问题描述】:
我很好奇为什么下面的代码 sn-p 不能编译:
typedef struct Foo {
int a;
int b;
} Foo;
static const Foo FooZero = { 0, 0 };
typedef struct Bar {
Foo foo;
int c;
} Bar;
static const Bar BarZero = { FooZero, 0 };
它抱怨FooZero的使用,指出FooZero不是Compile-Time Constant
但不是吗?我在这里不明白什么?
显然,我可以简单地将初始化程序中 FooZero 的使用替换为 { 0, 0 } - 我提出问题的目的不是如何解决问题 - 我试图了解 @987654329 的根本原因@ 实际上不是编译时常量。
谢谢
【问题讨论】:
-
stackoverflow.com/questions/3025050/… 是一个很好的解释,封装了我要写的内容
-
@Foo Bah - 感谢您的链接。然而;我的问题是“为什么会这样”。我觉得你引用的问题的答案并没有真正回答我“为什么”的问题——他们只是指出了我已经知道的情况。
-
@Steve 如果不使用 C 中的函数,您就无法解决问题吗? FooZero 也不是编译时间常数。两个链接都是这么说的。
-
@Pavan;
Also FooZero is not compile time constant.是的,我知道了。我的问题是为什么会这样?为什么语言的机制是这样的?
标签: c struct initialization