数组大小和const [英] array size and const
本文介绍了数组大小和const的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
可能重复:结果
<一href=\"http://stackoverflow.com/questions/4589463/initialize-array-size-from-another-array-value\">initialize从另一个数组值数组大小
在C ++
const int a[]={1,2,3,4,5};
int b[a[2]];
int main()
{
return 0;
}
在code是2号线给错误;
但是,如果是像它下面编译后没有给出错误:
The code is giving error in line 2; However, if it is something like below it gives no error after compilation:
const int a=3;
int b[a];
int main()
{
return 0;
}
这是为什么?但是如果我定义数组b里面主要是在两种情况下好吗...
Why is that? however if i define array b inside main it is alright in both the cases...
推荐答案
由于在C ++中的数组大小必须的恒前pressions 的,而不仅仅是常量数据。阵列中的数据,即使常量,不是一个常量前pression。
Because in C++ array sizes must be constant expressions, not just constant data. Array data, even though const, is not a constant expression.
第二个版本是一个不断前pression。
Second version IS a constant expression.
这篇关于数组大小和const的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文