Learn C++
#include "arrayspointers/arrays.hpp"

Arrays in C++

//concept

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.

//how it works
1

Fixed size, contiguous

int a[5]; reserves five ints in adjacent memory. The size must be a compile-time constant for stack arrays.

2

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.

3

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.

4

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.

//example
#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;
}
Output
75
1The braced list initializes all four elements; sum starts at 0.
2The loop walks indexes 0..3, accumulating 90+80+70+60 = 300.
3Dividing by 4.0 (not 4) keeps the average floating-point: 75 prints.
//use cases
Fixed-size buffers and lookup tables with zero overhead
Legacy APIs that demand raw pointers plus a length
Understanding vector's internals — contiguous storage with bounds-checked access
Preferring std::array/vector in new code and raw arrays only at boundaries

Recommended resourcesAffiliate links · may earn us a commission

Recommended for learners

Structured coding coursesVideo courses that pair well with visual practice — pick one language and go deep.Browse courses Pro code editor & AI assistantWrite, debug and explain code faster while you practice the patterns here.Try it free Classic DSA interview booksThe reference books interviewers themselves learned from — great next to the visualizers.See the books

Practice what you learned

Re-implement each visualized algorithm in C++ yourself — building beats watching.