A multidimensional array stores data in rows and columns, or even layers, such as a marks table, a chess board or the pixels of an image. In C it is really an array of arrays, laid out in memory one row after another. Once you understand that layout, declaring, looping over and passing these arrays to functions all make sense.
In this lesson you will learn
- How to declare and initialise 2D and 3D arrays
- How to loop over rows and columns with nested loops
- What row-major order means and why it matters
- How to pass a 2D array to a function, including variable sizes
- The indexing mistakes that cause silent bugs
Tables, Grids and Beyond
A one-dimensional array is a single row of boxes. Many real problems have more structure:
- A class register: students × subjects (2D)
- A cinema seating plan: rows × seats (2D)
- A hotel: buildings × floors × rooms (3D)
- A colour image: height × width × colour channels (3D)
Each extra dimension adds one more index in square brackets. If you have not used plain arrays yet, start with Arrays in C.
Declaring and Indexing 2D and 3D Arrays
int marks[3][4]; /* 3 rows, 4 columns, 12 ints */
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}}; /* one inner brace pair per row */
int partial[2][3] = {{7}, {8, 9}}; /* missing values become 0 */
int auto_rows[][3] = {{1, 2, 3}, {4, 5, 6}, {7, 8, 9}}; /* row count worked out: 3 */
marks[r][c]means rowr, columnc. Both start at 0.- You may leave out the first size when you give initial values. Every other size is required.
The usual way to visit every element is a loop inside a loop: the outer loop picks a row, the inner loop walks across it.
#include <stdio.h>
/* 3 students x 4 subjects: a table of marks */
int main(void) {
int marks[3][4] = {
{78, 85, 69, 90},
{66, 74, 88, 71},
{92, 81, 77, 85}
};
const char *name[] = {"Rafi", "Mitu", "Sabbir"};
for (int s = 0; s < 3; s++) {
int total = 0;
for (int sub = 0; sub < 4; sub++) total += marks[s][sub];
printf("%-7s total %d\n", name[s], total);
}
for (int sub = 0; sub < 4; sub++) {
int best = marks[0][sub];
for (int s = 1; s < 3; s++)
if (marks[s][sub] > best) best = marks[s][sub];
printf("subject %d best mark %d\n", sub + 1, best);
}
return 0;
}
Output
Rafi total 322
Mitu total 299
Sabbir total 335
subject 1 best mark 92
subject 2 best mark 85
subject 3 best mark 88
subject 4 best mark 90
The first pair of loops goes across each row to total a student’s marks. The second pair swaps the loop order to go down each column and find the best mark in each subject.
How C Stores It: Row-Major Order
Memory is one long line of bytes, so C stores a 2D array row by row. The whole of row 0 comes first, then the whole of row 1, and so on.
#include <stdio.h>
/* Row-major layout: rows are stored one after another. */
int main(void) {
int grid[2][3] = {{1, 2, 3}, {4, 5, 6}};
int *flat = &grid[0][0];
for (int k = 0; k < 6; k++) printf("%d ", flat[k]);
printf("\n");
printf("grid[1][2] is element %td of the block\n", &grid[1][2] - &grid[0][0]);
printf("sizeof grid = %zu, sizeof grid[0] = %zu\n", sizeof grid, sizeof grid[0]);
return 0;
}
Output
1 2 3 4 5 6
grid[1][2] is element 5 of the block
sizeof grid = 24, sizeof grid[0] = 12
The element grid[r][c] sits at position r × columns + c from the start. So grid[1][2] is 1 × 3 + 2 = 5. This is also why looping row by row (outer loop over rows) is faster than column by column on big arrays: it reads memory in order, which the CPU cache handles well.
A 3D Array
#include <stdio.h>
/* 2 buildings x 3 floors x 4 rooms: count occupied rooms */
int main(void) {
int occupied[2][3][4] = {
{{1, 0, 1, 1}, {0, 0, 1, 0}, {1, 1, 1, 1}},
{{0, 1, 0, 0}, {1, 1, 0, 1}, {0, 0, 0, 1}}
};
for (int b = 0; b < 2; b++) {
int count = 0;
for (int f = 0; f < 3; f++)
for (int r = 0; r < 4; r++)
count += occupied[b][f][r];
printf("building %d: %d of 12 rooms occupied\n", b + 1, count);
}
return 0;
}
Output
building 1: 8 of 12 rooms occupied
building 2: 5 of 12 rooms occupied
Three dimensions need three nested loops. Read occupied[b][f][r] as “building b, floor f, room r”.
Matrix Programs in C
To pass a 2D array to a function, the function must know the size of every dimension except the first, so it can work out where each row starts. Since C99, you can pass those sizes as parameters first and use them in the array type. This is called a variable-length array parameter.
#include <stdio.h>
/* C99 variable-length array parameter: pass the sizes first. */
static void print_matrix(int rows, int cols, int m[rows][cols]) {
for (int r = 0; r < rows; r++) {
for (int c = 0; c < cols; c++) printf("%4d", m[r][c]);
printf("\n");
}
}
static void transpose(int rows, int cols, int in[rows][cols], int out[cols][rows]) {
for (int r = 0; r < rows; r++)
for (int c = 0; c < cols; c++)
out[c][r] = in[r][c];
}
int main(void) {
int a[2][3] = {{1, 2, 3}, {4, 5, 6}};
int t[3][2];
transpose(2, 3, a, t);
print_matrix(2, 3, a);
printf("transposed:\n");
print_matrix(3, 2, t);
return 0;
}
Output
1 2 3
4 5 6
transposed:
1 4
2 5
3 6
Note the order: rows and cols must come before the array in the parameter list, because the array’s type uses them. Older code often fixes the column count instead, as in void print(int m[][3], int rows), which works only for 3-column arrays. More options are covered in passing a 3D array to a function.
Wrong Dimensions and Out-of-Range Indexes
- Going past the end. In
int m[3][4], valid rows are 0 to 2 and columns 0 to 3. C does not check, som[0][4]quietly readsm[1][0], andm[3][0]reads memory outside the array. - Writing
m[i, j]. In C this is the comma operator: it meansm[j], a whole row. Always writem[i][j]. - Swapping the loop limits. Using the column count for rows, or the reverse, is the most common bug in matrix code. Name your variables
rowsandcols, notnandm. - Passing to an
int **parameter. A 2D array is not an array of pointers.int a[3][4]cannot be passed asint **; useint a[][4]or a VLA parameter. - Huge local arrays.
double big[2000][2000]inside a function needs about 32 MB of stack, which can crash. Usestaticor allocate it withmalloc.
Practice Exercises
Exercise 1. How many bytes does double t[4][5] use if a double is 8 bytes?
Show answer
4 × 5 × 8 = 160 bytes.
Exercise 2. In int m[4][6], how many elements from the start is m[2][3]?
Show answer
2 × 6 + 3 = 15.
Exercise 3. Write the loop that adds the main diagonal of a square int a[n][n].
Show answer
int sum = 0;
for (int i = 0; i < n; i++)
sum += a[i][i];
Only one loop is needed, because the row and column are equal on the diagonal.
Frequently Asked Questions
Is a 2D array the same as an array of pointers?
No. A 2D array is one solid block of memory. An array of pointers (like char *names[3]) holds addresses that can point to rows anywhere in memory, each with its own length.
Why can I leave out only the first dimension?
To find m[r][c], the compiler multiplies r by the row length. It needs every size after the first to do that, but it never needs the number of rows.
How do I create a 2D array whose size is read at run time?
Either declare a variable-length array like int m[rows][cols]; for small sizes, or allocate one block with int (*m)[cols] = malloc(rows * sizeof *m);, which you can still index as m[r][c].
Is there a limit on the number of dimensions?
The C standard requires compilers to support at least 12. In practice, beyond 3 dimensions code becomes hard to read and a struct is usually clearer.
Multidimensional Arrays Summary
- A 2D array is an array of rows;
a[r][c]picks rowr, columnc. - C stores rows one after another (row-major), so
a[r][c]is elementr × cols + c. - Use nested loops, with the outer loop over rows for speed.
- Functions need every dimension except the first; VLA parameters make the sizes flexible.
- C does not check indexes, so keep row and column limits straight.
