小编Sye*_*yar的帖子

为什么全局数组大小应该是整数常量?

在 C++ 中,我尝试声明某个大小的全局数组。我得到了错误:

数组绑定不是 ']' 标记之前的整数常量

但是当我在main()函数中声明一个相同类型的数组时,它工作正常。

为什么这里有不同的行为?

int y=5;
int arr[y];         //When I comment this line it works fine

int main()
{
    int x=5;
    int arr2[x];        // This line doesn't show any error.
}
Run Code Online (Sandbox Code Playgroud)

编辑:许多人认为这个问题是重复获取错误 "array bound is not an integer constant before ']' token"。但是这个问题并没有回答为什么会有不同的行为。

c++ variable-length-array

8
推荐指数
1
解决办法
472
查看次数

标签 统计

c++ ×1

variable-length-array ×1