Arrays in C++
A C-style array is a fixed-size run of same-typed elements laid out contiguously: int scores[5]; reserves five ints back to back. Indexing starts at 0, and scores[i] is genuinely pointer arithmetic (address of scores plus i elements) — which is why out-of-bounds access compiles fine and then corrupts memory silently.
Arrays decay to pointers when passed to functions, losing their size — always pass the length alongside, or prefer std::array/vector. Braced initialization (int a[3]{1,2,3};) fills elements in order, and any missing tail elements are zero-initialized.
Fixed size, contiguous
int a[5]; reserves five ints in adjacent memory. The size must be a compile-time constant for stack arrays.
Zero-based indexing
Valid indexes run 0 to size-1. There is no bounds checking — a[5] on a 5-array reads чужой memory as undefined behavior.
Braced initialization
int a[5]{1, 2}; sets the first two and zero-fills the rest; int a[]{1,2,3} deduces size 3 from the list.
Arrays decay in calls
Passing an array to a function hands over just a pointer — the size is lost, so pass it as a separate parameter.
#include <iostream> int main() { int scores[4]{90, 80, 70, 60}; int sum = 0; for (int i = 0; i < 4; ++i) { sum += scores[i]; } std::cout << sum / 4.0 << std::endl; return 0;}Recommended resourcesAffiliate links · may earn us a commission
Recommended for learners
Practice what you learned
Re-implement each visualized algorithm in C++ yourself — building beats watching.