2D Arrays

Lesson 8 of 20

Rows times columns: address a grid with [row][col], fill it with nested loops and see row-major memory in 3D.

The big picture

A grid of cells

A 2D array is a grid: one index picks the row, the other picks the column — boards, matrices and tables.

A grid of cells

int grid[2][3] reserves 2 rows x 3 columns — six ints in one block.

Two indexes

grid[row][col] — both count from 0, so grid[1][2] is row 1, column 2.

Nested loops

The outer loop walks the rows; the inner loop walks the columns of each row.

Row-major memory

The grid flattens in memory: row 0 stored first, then row 1 — one continuous line.

Where you will use it

Game boards, matrices, spreadsheets, images — all of them are grids.