动态数组大小分配

Dynamic array size allocation

我用 C 语言编写了以下代码。我预计会出现错误消息,因为无法动态分配数组大小。但是代码可以编译。但是 myArray1 的结果大小是荒谬的。我不确定为什么要编译。我正在使用代码块和 minGW。

   int a;
    printf("Enter the value for a\n");
    scanf("%d",&a);
    int myArray2[a];
    printf("value of a = %d\tSize of myArray1 = %d",(sizeof(myArray2)/sizeof(myArray2[0])));

C99 标准支持堆栈上可变大小的数组。

这是上面的gcc docs

Variable-length automatic arrays are allowed in ISO C99, and as an extension GCC accepts them in C90 mode and in C++. These arrays are declared like any other automatic arrays, but with a length that is not a constant expression. The storage is allocated at the point of declaration and deallocated when the block scope containing the declaration exits.