I have been trying to learn arrays and I can't seem to define an array.
const int size = 10;
??[size];
Can anybody spot my mistakes? Thank You.
I have been trying to learn arrays and I can't seem to define an array.
const int size = 10;
??[size];
Can anybody spot my mistakes? Thank You.
To declare an array using a constant, you should use this syntax:
// constexpr is explicitly compile time constant
constexpr int size = 10;
int my_array[size]; // int array, can be changed to float
For dynamic array, use vector:
int size = ...;
std::vector<int> my_array;
array.resize(size);
You can also use modern statically sized array:
constexpr int size = 10;
std::array<int, size> my_array;
const int size = 10;
type array[size];
Is what you want. Substitute type
for any type (i.e. int
, float
, bool
etc.).
In this case, specifying array size using a const
variable is valid, but there are many cases where it isn't. You'd probably want to learn the difference between const
and constexpr
.
Also, instead of learning by asking questions here, try finding a good c++ source: a book, online article series or a video tutorial. It'll definitely accelerate your education.