开发者

In C++ books, array bound must be constant expression, but why the following code works?

开发者 https://www.devze.com 2023-03-04 16:01 出处:网络
#include <iostream> using namespace std; int main(){ int n=10; int a[n]; for (int i=0; i<n; i++) {
#include <iostream>
using namespace std;

int main(){
    int n=10;
    int a[n];

    for (int i=0; i<n; i++) {
        a[i]=i+1;
        cout<<a[i]<<endl;
}
    return 0;
}

worked fine in Xcode4 under Mac

as said in books, it shoul开发者_高级运维d be wrong, why?

so confused~


This a a C99 feature called VLA which some compilers also allow in C++. It's allocation on stack, just as it would be with int a[10].


That is C99 feature that allows VLA (variable length array).

Compile it with g++ -pedantic, I'm sure that wouldn't compile.

0

精彩评论

暂无评论...
验证码 换一张
取 消