[英]Difference between static, auto, global and local variable in the context of c and c++
[英]Global - Local difference in the init of an array in c
为什么这不起作用:
int size = 2;
int array[size];
int main() {
return 0;
}
它说错误: array bound is not an integer constant
这项工作:
int size = 2;
int main() {
int array[size];
return 0;
}
谁知道原因? 谢谢
在C ++或C89 / 90中都不起作用。 这些语言要求数组大小是积分常量表达式(ICE)。 在您的示例中, size
不是ICE。 如果您的C ++或C89 / 90编译器允许它,它只不过是非标准的编译器扩展。
在C99中,第二个工作,因为这是定义可变阵列长度(VLA)规范的方式。 VLA只能在本地范围内定义。
动态大小的数组是C99的一个特性,根本不包含在当前的C ++标准中。
如果你正在编译C ++也不应该工作。 如果更改为const变量,那么C ++标准将允许这两种方法。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.