How to use the the value stored in a int variable as the size of my array
eg.
int a = 40;
int b[a]; // a = 40
How to use the the value stored in a int variable as the size of my array
eg.
int a = 40;
int b[a]; // a = 40
You can't; not in standard C++ anyway, unless a
is a constexpr
or const
integral type with a positive value.
The best alternative is a std::vector<int> b(a);
Create an array with new[]
:
int *b = new int[a];
but remember to delete it later with:
delete[] b;
A better alternative is the std container, like std::vector
.