我已经看了这些,他们不回答我的问题:GCC抱怨:可变大小的对象可能不被初始化
variable-sized object may not be initialized
C compile error: "Variable-sized object may not be initialized"
Error: Variable-sized object may not be initialized. But why?
我试图写一些相当便携的代码:
int main()
{
const int foo=13;
int bar[foo]={0};
return 0;
}
使用作为c
代码在编译时,我得到一个错误variable-sized object may not be initialized
既:
- GCC 4.3.4
- ARM-Linux的gnueabi-GCC 4.4.5
如果我编译为c
VS2008我得到一个略有不同error C2057: expected constant expression
据我所知,在这里,c
代码编译器不承认const int foo=13;
是真正的恒定;例如,我们可能有
void a(int fool)
{
const int foo=fool;
int bar[foo]={0};
}
我也意识到unlike the gcc compilers的VS2008编译器有没有C99 variable-length arrays概念。而且这位MS显然没有提到任何未来的支持。
然而,cpp
有两种GCC或MS编译器代码编译是完全不同的/聪明?!
并且还什么我不明白关于GCCc
代码编译为:
(注:在最后一种情况下,MSc
代码编译失败,始终与int bar[foo]={0};
)
在我的TDM-GCC 4.6.1上,最后一个例子没有**编译(再次使用“可变大小的对象不能被初始化”)。我也从来没有听说过由初始化列表初始化的VLA,这是否正确?望着它.. – Anthales 2012-04-12 18:29:32
对于MS编译器问题的一部分,这不是唯一缺少的功能,它们只是不支持C99。自去年以来,甚至有更新版本的标准C11,所以他们现在缺少两个主要版本。 – 2012-04-12 18:45:30
@ [anthales](http://stackoverflow.com/users/1250595/anthales),谢谢。嗯.. – violet313 2012-04-12 18:48:19