Learning materials
Array Basics
On this page
Only local arrays (arrays declared as local variables) are discussed in this document.
-
Declaration and initialization:
int arr[10]; int arr1[10] = {}; int arr2[] = {1, 2, 3};-
size must be specified unless it can be inferred from initialization
-
size must be a compilation-time constant
- int literals:
10 - pre-defined const variable:
const int SIZE = 4
- int literals:
// ---- correct ---- const int SIZE = 4; int arr3[SIZE]; // ---- wrong ---- int size = 4; int arr4[size]; -
-
Access the element:
int sum = 0; int arr[4] = {1, 2, 3, 4}; cout << "The second element is " << arr[1] << endl; arr[1] = 12; cout << "Second element is set to " << arr[1] << endl;