Arrays in C
An 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 a[i] is genuinely pointer arithmetic (address plus i elements) — which is why out-of-bounds access compiles fine and then corrupts memory silently.
Arrays decay to pointers in most expressions (notably function calls), losing their size — always pass the length alongside. Braced initialization (int a[3] = {1,2,3};) fills in order with missing tails zeroed, and int a[] = {1,2,3}; deduces size 3.
Fixed size, contiguous
int a[5]; reserves five ints adjacently. Stack array sizes must be compile-time constants (except VLAs).
Zero-based, unchecked
Valid indexes run 0 to size-1. No bounds checking exists — a[5] on a 5-array reads unrelated 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.
Decay loses size
Passing an array hands over just a pointer — the callee can't know the length, so pass it as a separate parameter.
#include <stdio.h> int main(void) { int scores[4] = {90, 80, 70, 60}; int sum = 0; int i; for (i = 0; i < 4; ++i) { sum += scores[i]; } printf("%.1f\n", sum / 4.0); 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.