Sub topic 8
Two-Dimensional Arrays and Pointer Equivalents
This chapter assumes you know how one-dimensional arrays decay to pointers, how pointer arithmetic scales by element size, and how sizeof behaves on arrays. Here we apply those rules to arrays of arrays. GATE questions on this topic mostly test three things: the type of each sub-expression, the arithmetic scale, and the address formula.
A 2D array is an array of arrays
int a[3][4] declares an array of 3 elements, and each element is an int[4]. There are no row pointers stored anywhere. The compiler lays all 12 int values in one contiguous block in row-major order: row 0 first, then row 1, then row 2.
For an array with N columns and element size w, the address of a[i][j] is:
base + w * (i * N + j)
For example, take int a[10][20] at base address 1000 with 4-byte ints. The address of a[3][5] is 1000 + 4 × (3 × 20 + 5) = 1260. Note that the number of rows never appears in the formula. The column count is what the compiler needs.
Types and decay
Decay converts an array expression into a pointer to its first element. Applying that rule to a 2D array gives a pointer to a whole row, not a pointer to an int.
| Expression | Type before decay | Type after decay (in most contexts) |
|---|---|---|
| `a` | `int[3][4]` | `int (*)[4]` |
| `a[1]` or `*(a+1)` | `int[4]` | `int *` |
| `a[1][2]` | `int` | not applicable |
| `&a` | `int (*)[3][4]` | not applicable |
| `&a[1]` | `int (*)[4]` | not applicable |
Decay does not happen under sizeof or unary &. That is why sizeof a gives the whole array and sizeof a[0] gives one row.
Because a decays to int (*)[4], a + 1 advances by sizeof(int[4]), which is 16 bytes, so it points at row 1. Applying * to it gives the row itself, and adding to that row moves by single ints.
This gives the central equivalence:
a[i][j] is *(*(a + i) + j)
Checking it in code
The program assumes a 4-byte int.
#include <stdio.h>
int main(void) {
int a[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
printf("%zu %zu %zu\n", sizeof a, sizeof a[0], sizeof a[0][0]);
printf("%d %d %d\n", *(*(a + 1) + 2), *(a[2] + 1), (*(a + 1))[3]);
int *flat = &a[0][0];
printf("%d\n", flat[6]);
printf("%td\n", (char *)(a + 1) - (char *)a);
return 0;
}The output is:
48 16 4
7 10 8
7
16*(*(a + 1) + 2)isa[1][2], which is 7.*(a[2] + 1)isa[2][1], which is 10.(*(a + 1))[3]isa[1][3], which is 8.flat[6]treats the block as 12 consecutive ints, so index 6 is row 1, column 2, which is 7.a + 1is exactly 16 bytes pasta.
Passing a 2D array to a function
A function receiving a 2D array must know the column count. The parameter int p[][4] is adjusted to int (*p)[4], so both forms below mean the same thing.
#include <stdio.h>
void print_rows(int (*p)[4], int n) {
for (int i = 0; i < n; i++) {
for (int j = 0; j < 4; j++) {
printf("%d ", p[i][j]);
}
printf("\n");
}
}
int main(void) {
int a[2][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}};
print_rows(a, 2);
return 0;
}The first dimension is lost by decay, so sizeof p inside print_rows is the size of a pointer. Only the first dimension is lost. The inner dimension 4 stays in the type, which is why p[i][j] can still compute the address.
The parentheses in int (*p)[4] matter. Without them, int *p[4] is an array of 4 pointers to int, which is a different type.
Array of pointers versus 2D array
int *rows[3] looks similar in use, since rows[i][j] also works, but the memory model is different.
#include <stdio.h>
int main(void) {
int r0[] = {1};
int r1[] = {2, 3};
int r2[] = {4, 5, 6};
int *rows[3] = {r0, r1, r2};
printf("%zu %d\n", sizeof rows, rows[2][1]);
return 0;
}On a 64-bit system this prints 24 5. The array rows holds only three pointers, and the rows it points to can have different lengths (a jagged array) and live anywhere in memory. To evaluate rows[i][j], the machine first loads the pointer rows[i] from memory and then indexes from it. For a true 2D array, a[i][j] needs no such load because the address comes from arithmetic alone.
| Property | `int a[3][4]` | `int *rows[3]` |
|---|---|---|
| Storage | One contiguous block | Three pointers plus separate rows |
| `sizeof` | 48 (4-byte int) | 3 × pointer size |
| Row lengths | All equal | Can differ |
| `a[i]` type | `int[4]` | `int *` |
| Decays to | `int (*)[4]` | `int **` |
Common errors
- Assigning to `int
.**int *pp = a;is a type mismatch.adecays toint ()[4], and a pointer to an array of 4 ints is not a pointer to a pointer. Even with a cast,pp[1]` would read integer data as if it were an address. - Wrong scale. If
pisint (*)[4], thenp + 1moves 16 bytes, not 4. Ifqisint *, thenq + 1moves 4 bytes. - Missing column size.
void f(int p[][])does not compile, because the compiler cannot computei * NwithoutN. - Confusing
aand&a. Both have the same address value, buta + 1moves one row (16 bytes) while&a + 1moves the whole array (48 bytes). - Assuming
sizeofworks on a parameter. Inside a function,sizeof pis a pointer size, not the array size.
Exercise
Given int b[2][3] = {{1, 2, 3}, {4, 5, 6}};, with 4-byte int and 8-byte pointers, what are the values of *(b[1] + 2), sizeof(*b), and sizeof(b + 1)?
Show answer
b[1] is the row {4, 5, 6}, which decays to a pointer to 4, so *(b[1] + 2) is 6. *b is b[0], an int[3] that is not decayed under sizeof, so sizeof(*b) is 12. b + 1 is a pointer of type int (*)[3], so sizeof(b + 1) is 8.
Exercise
A short s[5][7] array (2-byte elements) starts at address 2000. What is the address of s[3][2]?
Show answer
The address is 2000 + 2 × (3 × 7 + 2) = 2000 + 2 × 23 = 2046. Only the column count, 7, enters the formula.