大小的数组由不恒定的变量定义 [英] Array of size defined by not constant variable
本文介绍了大小的数组由不恒定的变量定义的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
有这样code:
#include <iostream>
int main()
{
int size;
std::cin >> size;
size = size + 1;
int tab3[size];
tab3[0] = 5;
std::cout << tab3[0] << " " << sizeof(tab3) << std::endl;
return 0;
}
的结果是:
$ g++ prog.cpp -o prog -Wall -W
$ ./prog
5
5 24
为什么这个code编译连?不应该数组的长度常数变量?
Why does this code even compile? Shouldn't be length of array a constant variable?
我用G ++版本4.4.5。
I used g++ version 4.4.5.
推荐答案
用C变长数组++都可以作为GCC的扩展。与所有的警告编译应该提醒你这个事实(包括 -pedantic
)。
Variable-length arrays in C++ are available as an extension in GCC. Compiling with all warnings should have alerted you to that fact (include -pedantic
).
这篇关于大小的数组由不恒定的变量定义的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文