double x[3]; // 3 doubles, uninitialized
int y[5] = {10, 15, 20, 25, 30}; // 5 integers with the given values
x[0] = 1.5; // assigns to first element of x
cout << y[4] << end; // prints last element of y
- C++ inherits from C a more basic, less flexible way of storing a sequence of values: the array
- Arrays are defined like regular variables, with the addition of
[]
containing array size
- Array size can be a literal integer value, an integer constant or an expression that can be evaluated to an integer at compile time
- Initial values for the array elements can be supplied in { }, separated by commas
- Unlike vectors, arrays cannot grow or shrink after they are created
[]
is used to index array elements, just as it is for vectors