非固定大小的数组:为什么这甚至工作? [英] Array of non-constant size: Why does this even work?

查看:92
本文介绍了非固定大小的数组:为什么这甚至工作?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

#include <iostream>
using namespace std;

int main(){
    int n;
    cout<<"Enter the size :";
    cin>>n;
    int array[n];  // I've worked some outputs and it works 
    return 0;
}

这是一些动态分配的?结果
为什么没有它甚至给出了N是一个错误常量?

Is this some kind of dynamic allocation?
Why doesn't it even gives an error for 'n' to be a "const"?

此外,写 COUT&LT;&LT;数组[N + 5]; 不会导致编译或运行时错误

Also, writing cout << array[n+5]; doesn't result in an compile time or runtime error.

我使用的开发-C ++。

I'm using Dev-C++.

推荐答案

显然可以在C99声明变长数组,似乎的 GCC然后接受了C ++也的。

Apparently one can declare variable length arrays in C99, and it seems GCC accepts then for C++ also.

变长自动数组被允许在ISO C99,并作为
  扩展GCC接受他们C90模式和C ++。这些阵列
  宣布像任何其他自动数组,但具有的长度是
  不是一个常数前pression。

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.

您学到东西,每天..我从来没有看到过。

You learn something every day .. I hadn't seen that before.

这篇关于非固定大小的数组:为什么这甚至工作?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆