Rows and Columns in Memory
A cabinet with rows and columns
The two indexes select a row and then an element within that row. The diagram connects this grid view to its storage order:
Picture two shelves with three bins each. int bins[2][3] has two rows, and each row holds three integers. bins[1][2] is the last bin on the second shelf. In memory, C lays out all of row 0 before row 1. Valid row indexes are 0 and 1; valid column indexes are 0, 1, and 2.
#include <stdio.h>
int main(void) {
int bins[2][3] = {{2, 4, 6}, {1, 3, 5}};
for (int row = 0; row < 2; row++) {
for (int column = 0; column < 3; column++) {
printf("%d ", bins[row][column]);
}
printf("\n");
}
return 0;
}
Before running, trace row 0: it prints 2, 4, 6. The inner loop finishes, printf("\n") ends the line, and the outer loop moves to row 1. Keep each loop bound matched to its dimension; mixing them up can take an index outside the array. Contiguous rows do not make bins[0][3] a valid way to access the next row: the column is outside row 0. Use bins[1][0]. The diagram shows element order, not measured addresses or a required byte size for int.