Multidimensional Arrays
A multidimensional array in C is an array of arrays. int m[2][3] is two rows of three ints, stored row-major in one contiguous block.
Detailed Explanation
int m[2][3] has type "array of 2 arrays of 3 int". The inner size is part of the element type. Layout is row-major: m[0][0], m[0][1], m[0][2], m[1][0], m[1][1], m[1][2] sit in consecutive addresses. That is the same order C uses to initialize with nested braces: int m[2][3] = {{1, 2, 3}, {4, 5, 6}};. A flat initializer {1, 2, 3, 4, 5, 6} fills in the same order.
Decay happens one level at a time. In an expression, m becomes a pointer to its first element, which has type int (*)[3] — pointer to array of 3 int. m[0] decays further to int *. This is why a function that accepts a real 2-D array must know the inner dimension: void f(int m[][3], size_t rows) or void f(int (*m)[3], size_t rows). The outer size can be omitted. The inner size cannot, because the compiler needs it to compute m[1].
An array of pointers is a different object. int *rows[2]; is two pointers that may address anywhere. You can build a "jagged" table that way, with rows of different lengths, and the rows need not be adjacent. You also pay an extra indirection and you lose a single sizeof-based size. Do not confuse int m[2][3] with int **p. A function declared void f(int **p) cannot receive m. The types do not match, and a cast will not make the arithmetic right.
Dynamic 2-D storage has two honest shapes. One allocation of rows * cols elements, indexed as a[i * cols + j], is cache-friendly and easy to free. An array of pointers, each malloc'd, is easier to resize per row and harder to clean up. A VLA parameter void f(size_t r, size_t c, int m[r][c]) is readable and optional as of C11; many projects avoid it for portability.
When you pass a matrix to a library that wants a flat buffer — BLAS, image code, serializers — use the contiguous layout and document whether the stride is row-major. C will not rearrange it for you.
Code Examples
#include <stdio.h>
static void print_matrix(size_t rows, const int m[][3])
{
for (size_t i = 0; i < rows; i++) {
printf("%d %d %d\n", m[i][0], m[i][1], m[i][2]);
}
}
int main(void)
{
int m[2][3] = {
{1, 2, 3},
{4, 5, 6},
};
print_matrix(2, m);
/* prints:
1 2 3
4 5 6 */
int *first_row = m[0]; /* decays to int * */
printf("%d\n", first_row[5]); /* prints: 6 — still in bounds of the whole object */
return 0;
}#include <stdio.h>
#include <stdlib.h>
static int *matrix_new(size_t rows, size_t cols)
{
if (cols != 0 && rows > (size_t)-1 / cols / sizeof(int)) {
return NULL;
}
return calloc(rows * cols, sizeof(int));
}
int main(void)
{
size_t rows = 2, cols = 3;
int *a = matrix_new(rows, cols);
if (a == NULL) {
return 1;
}
a[1 * cols + 2] = 42; /* element [1][2] */
printf("%d\n", a[5]); /* prints: 42 */
free(a);
return 0;
}Common Gotchas
Only the inner dimension is part of the decayed pointer type
`void f(int m[][3])` is fine; `void f(int m[][])` is not, except as a VLA with extra parameters.
`m[0][5]` on an `m[2][3]` can be in bounds of the underlying object and still be a type-level out-of-bounds on the inner array
C99 made one-past the inner array a grey area; prefer `m[1][2]`.
An array of separately allocated rows needs a free per row plus a free of the pointer array
missing one is a leak; freeing in the wrong order is a use-after-free.