2D Arrays

Lesson 8 of 20

Rows times columns: build a grid of values, index it with two numbers, and see how it flattens into memory.

The big picture

A grid of boxes

A 2D array is a grid: rows and columns of boxes. Two indexes pick one cell — and memory still stores it all in one line.

Declaration

int m[2][3] reserves 2 rows of 3 columns — six boxes in total.

Two indexes

m[r][c]: the first bracket picks the row, the second the column. Both count from 0.

Nested loops

One loop per dimension: the outer walks rows, the inner walks each row's cells.

Row-major memory

C lays the grid out row after row: m[0][0], m[0][1], m[0][2], m[1][0], ...