-9

I have been asked to prove that one can set the array length in the program, and it does not need to be determined at compile time. I have following code:

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

But when I compile, I get the following errors:

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

I'm not sure what the right way to approach it.

Sourav Ghosh
  • 133,132
  • 16
  • 183
  • 261
user3922546
  • 187
  • 1
  • 6
  • 16

1 Answers1

1

int matrix[x]; i dont think c++ does not support that kind of declaration. use new[] operator if you want to dynamically allocate memory for an array