Array (C++)

A C++ array is a fixed-size, contiguous block of elements that all share the same type. You access elements by a zero-based index in square brackets.

C++

The size of a built-in array is fixed when you declare it and cannot change. Indexing starts at 0, so an array of size 5 uses indices 0 through 4 — going outside that range is undefined behavior.

For a resizable, safer alternative, most modern C++ code uses std::vector instead of raw arrays.

Syntax

type name[size] = { values };

Examples

C++

#include <iostream>

int main() {
    int nums[3] = {10, 20, 30};
    std::cout << nums[0] << " " << nums[2] << std::endl;
    return 0;
}

Output:

10 30

Learn it in a lesson

See also

Back to the Dictionary