【发布时间】:2015-06-15 16:53:28
【问题描述】:
我很惊讶下面的代码编译得很好。我一直认为数组大小在编译时必须是一个常数,但似乎我可以接受用户的输入并将其用作数组大小。我正在将 GCC 与代码块 IDE 一起使用。有没有人尝试过,这样做有什么问题吗?
int size;
cout<<"Enter array size : "<<endl;
cin>>size;
int arr[size];
// ...more action array with the array after which works fine
【问题讨论】:
-
首先,这不是 C,是的,它完全有效。
-
相关:VLAs in C++
-
@OldProgrammer 不,它在 C++ 中无效,在 C 中声明 VLA 是有效的,但 C++ 没有它。