-9

我被要求证明可以在程序中设置数组长度,并且不需要在编译时确定。我有以下代码:

#include<iostream>
using namespace std;
int main()
{
    int x, myarray[x];
    cout << "Enter the size of array" << endl;
    cin >> x;
        }

但是当我编译时,我得到以下错误:

error C2057: expected constant expression
 error C2466: cannot allocate an array of constant size 0
 error C2133: 'myarray' : unknown size

我不确定处理它的正确方法是什么。

4

1 回答 1

1

整数矩阵[x];我不认为 c++ 不支持这种声明。如果要为数组动态分配内存,请使用 new[] 运算符

于 2014-10-13T16:29:25.187 回答