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

前端 未结 2 1899
感情败类
感情败类 2020-11-22 13:13
#include 
using namespace std;

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

    for (int i=0; i

        
相关标签:
2条回答
  • 2020-11-22 13:53

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

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

    0 讨论(0)
  • 2020-11-22 14:14

    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].

    0 讨论(0)
提交回复
热议问题