dynamic array with depends on user entry value

hi everyone, i am new cpp forum

my question is :

int a;

cin>>a;

int array[a];

is this proper assignment ?
It's illegal in C++ to do this - though it will work in GCC.
In C++ the dimesions of an array must be specified at compile time.
If you don't know the size of an array then use a std::vector.
Not in C++ (at least, not according to the current standard, since it is setting a standard array size at run-time; some compilers allow it).

It is valid in C (from C99) and in many other languages.

For a dynamic array you can use
int *array = new int[a];
Last edited on
firstly thank you for answer all of you, the cpp forum is good,i understand i am using dev cpp 5.11 and it allows that, if i understand correctly i should use that int *array = new int[a];
i understand correctly i should use that int *array = new int[a];
Not really - unless it's a homework.
In modern C++ you better would use a std::vector:
1
2
3
int a;
cin>>a;
std::vector<int> vi(a);

The vector will manage the memory for you.
i will search the vector it will be my homework and i will do it to learn

thank you
Topic archived. No new replies allowed.