← Strings and ArraysC PROGRAMMING 1.3.B
Multidimensional Arrays
Row/column layout and access patterns for 2D arrays.
Five pieces on 2D arrays — mostly about realizing there's no such thing as a real "2D" array in memory, just a flat block laid out one row after another.
int grid[3][4];
grid[1][2] = 7; /* row 1, column 2 */grid[3][4] is really one contiguous block of 12 ints. Row-major layout means the whole first row comes first in memory, then the whole second row, and so on — grid[1][2] is really just *(grid + 1*4 + 2) under the hood, the compiler doing the row-width multiplication automatically.
Passing a 2D array to a function needs the column count as part of the type — void f(int grid[][4]) — because the compiler needs that number to compute the row stride; the row count alone doesn't tell it anything.
Notes from readers
Comments — via GitHub