c++ - 为什么这个c++程序可以编译?

标签 c++ arrays compilation

<分区>

据我了解,C++ 编译器不允许您使用变量初始化数组,但此程序在我的计算机上编译并运行,这是为什么?

// this should not compile because there is a variable in the array declaration

#include <iostream>

int main(){
    int x = 5;
    int ar[x];
    printf("hello world\n");
}

最佳答案

In C++, variable length arrays are not legal. G++ allows this as an "extension" (because C allows it), so in G++ you can do this.

检查这个answer/answer进一步。

关于c++ - 为什么这个c++程序可以编译?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41369494/

相关文章:

c++ - 我怎样才能知道指针参数的来源?

c++ - 为什么此代码会出现段错误

php - 根据有序数组对多维数组进行排序

c++ - Boost编译错误

c++ - C/C++ : Compressing(gz) data without using temporary file

c++ - 如何最好地消除有关未使用变量的警告?

c++ - 使用调试器与构建和运行 C++ 时的输出不同

javascript - 使用 Nashorn 遍历数组

compiler-construction - 如何在 Sublime Text 2 中编译和运行 C 程序?

compilation - 如何修复 fatal error : zlib. h : no such file or directory?