Lesson 2 of 7
2D Arrays - Row-Major Layout & Matrix Operations
A 2D array in C is stored in **row-major order** - all elements of row 0 come first, then row 1, etc. `int m[3][4]` is 12 consecutive integers. When passing a 2D array to a function, you must specify all dimensions except the first - the compiler needs the column count to calculate element addresses.
2D Arrays - Memory, Traversal & Function Passing
#include <stdio.h>
#define ROWS 3
#define COLS 4
void print_matrix(const int m[][COLS], int rows) {
/* Must specify COLS - compiler needs: address = base + row*COLS + col */
for (int i = 0; i < rows; i++) {
for (int j = 0; j < COLS; j++) {
printf("%4d", m[i][j]);
}
printf("
");
}
}
void transpose(const int src[][COLS], int dst[][ROWS], int rows) {
for (int i = 0; i < rows; i++)
for (int j = 0; j < COLS; j++)
dst[j][i] = src[i][j];
}
int main(void) {
/* ── Declaration and initialisation ──────────────────── */
int mat[ROWS][COLS] = {
{ 1, 2, 3, 4},
{ 5, 6, 7, 8},
{ 9, 10, 11, 12}
};
printf("Matrix:
");
print_matrix(mat, ROWS);
/* ── Memory layout - row-major ───────────────────────── */
/* mat[0][0] mat[0][1] mat[0][2] mat[0][3] ← row 0 contiguous
mat[1][0] mat[1][1] mat[1][2] mat[1][3] ← row 1 contiguous
Address formula: &mat[i][j] = base + (i * COLS + j) * sizeof(int) */
printf("
Element addresses (row-major):
");
for (int i = 0; i < ROWS; i++)
for (int j = 0; j < COLS; j++)
printf("mat[%d][%d] @ %p
", i, j, (void *)&mat[i][j]);
/* ── Transpose ───────────────────────────────────────── */
int trans[COLS][ROWS]; /* note: dimensions swapped */
transpose(mat, trans, ROWS);
printf("
Transpose:
");
for (int i = 0; i < COLS; i++) {
for (int j = 0; j < ROWS; j++) printf("%4d", trans[i][j]);
printf("
");
}
return 0;
}Common Mistakes
- Passing 2D arrays without column size - `void f(int m[][], int rows)` is a compile error. The compiler cannot compute addresses without knowing column count: `void f(int m[][COLS], int rows)`.
- Row-major vs column-major confusion - C is row-major. Iterating `j` in the outer loop and `i` in the inner loop (column-major access) causes poor cache performance for large matrices - every access jumps by `COLS * sizeof(int)` bytes, causing cache misses.
- Thinking 2D arrays are arrays of pointers - `int m[3][4]` is a single contiguous block of 12 ints. `int **m` is an array of pointers. They are not interchangeable in function parameters.
Tip
Tip
Practice 2D Arrays RowMajor Layout Matrix Operations in small, isolated examples before integrating into larger projects. Breaking concepts into small experiments builds genuine understanding faster than reading alone.
C stores 2D arrays row-by-row in a single contiguous memory block
Practice Task
Note
Practice Task - (1) Write a working example of 2D Arrays RowMajor Layout Matrix Operations from scratch without looking at notes. (2) Modify it to handle an edge case (empty input, null value, or error state). (3) Share your solution in the Priygop community for feedback.
Quick Quiz
Common Mistake
Warning
A common mistake with 2D Arrays RowMajor Layout Matrix Operations is skipping edge case testing - empty inputs, null values, and unexpected data types. Always validate boundary conditions to write robust, production-ready c code.
Key Takeaways
- A 2D array in C is stored in **row-major order** - all elements of row 0 come first, then row 1, etc.
- Passing 2D arrays without column size - `void f(int m[][], int rows)` is a compile error. The compiler cannot compute addresses without knowing column count: `void f(int m[][COLS], int rows)`.
- Row-major vs column-major confusion - C is row-major. Iterating `j` in the outer loop and `i` in the inner loop (column-major access) causes poor cache performance for large matrices - every access jumps by `COLS * sizeof(int)` bytes, causing cache misses.
- Thinking 2D arrays are arrays of pointers - `int m[3][4]` is a single contiguous block of 12 ints. `int **m` is an array of pointers. They are not interchangeable in function parameters.
Finished this lesson?