InfoCode

C++ · Arrays

Array index

Learn C++ array initialization and zero-based indexing, then check valid bounds with element access and reverse traversal.

Written by InfoCode · Technical references below

Practice this topic in the app Read the lesson in the app

Array length and valid indices

Declare an array int a[5];, meaning there are 5 cells, numbered 0, 1, 2, 3, 4 (not 1 to 5!).

Access elements with a[index], for example a[0] is the first element, and a[4] is the last one.

Analogy: from the 0th floor (basement) to the 4th floor of a building, 5 floors in total, but the highest is written as a[4].

The index can be a variable, for example a[i]—this is the core way to traverse an array with a loop.

Out-of-bounds index: accessing a[-1] or a[5] (for an array of size 5) is out of bounds, and the program behavior is undefined (it may crash, or it may output strange values).

Why is the last index 3 for four elements?

Draw int a[4] = {8, 3, 5, 2}; as four slots: indices 0, 1, 2 and 3 store 8, 3, 5 and 2. Length 4 counts the elements; it is not the last index. Predict the last element and the sum, then trace each access:

ii < 4Element accessedsum after adding
0Truea[0] = 88
1Truea[1] = 311
2Truea[2] = 516
3Truea[3] = 218
4False; stopDo not access a[4]18

First error: i <= 4 attempts to access a nonexistent fifth element. Second error: treating a[-1] as the last element; ordinary C++ arrays have no such reverse-index rule. An out-of-bounds access has undefined behavior. A run that does not crash is not proof of safety.

This example initializes every element. Do not assume an uninitialized ordinary local array contains zeros. Keep capacity separate from the number of valid entries: if an array has room for 100 entries but holds n, traverse only those n entries and require 0≤n≤100. Traversing n entries takes O(n); one valid ordinary-array subscript takes O(1).

Run and check: the last element and a complete traversal

#include <iostream>
int main() {
    int a[4] = {8, 3, 5, 2};
    int sum = 0;
    for (int i = 0; i < 4; ++i) { sum += a[i]; }
    std::cout << a[3] << " " << sum << "\n";
}

Output: 2 18. The loop exits when i reaches 4, without accessing a[4].

Try before revealing each answer

Is a[1] the last element of int a[1] = {9};?

No. The only valid element index is 0; a[0] is 9. Do not read a[1] as an element.

For a four-element array, what upper bound on i allows comparing a[i] and a[i+1], starting at 0?

Require i < 3: i is at most 2, so i+1 is at most 3. Checking only i < 4 is insufficient.

An array has capacity 100 but n=0 valid entries. How many iterations does an i < n traversal run?

Starting at i=0, it runs zero times and accesses no elements. This does not declare a zero-length ordinary array.

Element access and reverse traversal

Example 1: Basic usage of indices starting from 0

#include <iostream>
using namespace std;
int main() {
    int a[5] = {10, 20, 30, 40, 50};
    cout << a[0] << endl; // 10,the first element
    cout << a[2] << endl; // 30,the third element
    cout << a[4] << endl; // 50,the last element
    // a[5] is out of bounds; do not access it!
    return 0;
}

Array a has 5 elements, and the legal indices are 0~4. a[0]=10, a[2]=30, a[4]=50.

Example 2: Output the array in reverse order using indices

#include <iostream>
using namespace std;
int main() {
    int a[5] = {1, 2, 3, 4, 5};
    for (int i = 4; i >= 0; i--) {
        cout << a[i] << " ";
    }
    cout << endl; // output:5 4 3 2 1
    return 0;
}

i decreases from 4 to 0, accessing a[4], a[3]...a[0] in turn, realizing reverse-order output.

Tips

Prerequisites

Technical references

Scope note

This lesson is written by InfoCode and is not an official syllabus; the reference level is a site mapping and does not represent official levels, certification or eligibility. Practice records do not establish mastery or eligibility.

Reference level: GESP Level 3 (site mapping)

See contest pathways