AdvancedC Programming · Lesson 13 of 15 · Updated ·
Pointers and 2D Arrays in C — Array of Pointers, Pointer to Array, Dynamic Matrices
The pointers lesson showed that a[i] is *(a + i) for a one-dimensional array. This lesson takes the same idea to two-dimensional arrays (matrices) and to arrays of strings. It is where students most often confuse int *p[3] with int (*p)[3], and where many viva questions come from.
1. A 2D array is stored row by row
Declare a marks table for 3 students and 4 subjects:
int m[3][4];
The memory is one long line of 12 ints. Row 0 comes first, then row 1, then row 2. This is called row-major order. So the element m[i][j] is at position i × 4 + j from the start, where 4 is the number of columns.
#include <stdio.h>
int main(void) {
int m[3][4] = {
{1, 2, 3, 4},
{5, 6, 7, 8},
{9, 10, 11, 12}
};
int *flat = &m[0][0]; /* treat the block as one row */
printf("%d %d\n", m[1][2], flat[1 * 4 + 2]);
printf("%d\n", (int)(sizeof(m) / sizeof(m[0]))); /* rows */
printf("%d\n", (int)(sizeof(m[0]) / sizeof(m[0][0]))); /* columns */
return 0;
}
7 7
3
4
sizeof(m) is the whole table (48 bytes with 4-byte ints) and sizeof(m[0]) is one row (16 bytes). Dividing gives the number of rows.
2. What does m[i][j] really mean?
A 2D array is an array of arrays. m has 3 elements, and each element is a whole row of 4 ints.
min an expression becomes a pointer to the first row. Its type isint (*)[4]— "pointer to an array of 4 ints".m + 1jumps a whole row (16 bytes), not one int.*(m + i)is rowi, which then becomes a pointer to its first int.*(m + i) + jpoints to columnjof that row.
So m[i][j] is exactly *(*(m + i) + j).
#include <stdio.h>
int main(void) {
int m[3][4] = {{1, 2, 3, 4}, {5, 6, 7, 8}, {9, 10, 11, 12}};
printf("%d\n", *(*(m + 2) + 1)); /* m[2][1] */
printf("%d\n", **m); /* m[0][0] */
printf("%d\n", *(m[1] + 3)); /* m[1][3] */
printf("%d\n", (int)((char *)(m + 1) - (char *)m)); /* bytes in one row */
return 0;
}
10
1
8
16
3. Array of pointers vs pointer to an array
The brackets decide everything. Read declarations from the name outwards, and [] binds tighter than *:
| Declaration | Read as | Typical use |
|---|---|---|
int *p[3] | p is an array of 3 pointers to int | a list of strings, a "jagged" table |
int (*p)[3] | p is a pointer to an array of 3 ints | walking the rows of an int [n][3] matrix |
An array of pointers is perfect for a list of names, because each name can have a different length:
#include <stdio.h>
#include <string.h>
int main(void) {
const char *cities[] = {"Pune", "Delhi", "Kochi", "Jaipur"};
int n = sizeof(cities) / sizeof(cities[0]);
for (int i = 0; i < n; i++)
printf("%s has %d letters\n", cities[i], (int)strlen(cities[i]));
printf("%c\n", cities[1][0]); /* first letter of Delhi */
return 0;
}
Pune has 4 letters
Delhi has 5 letters
Kochi has 5 letters
Jaipur has 6 letters
D
Compare with char names[4][7]: a 2D char array reserves 7 bytes for every name, even short ones. The array of pointers stores only four addresses, and each string uses exactly the space it needs.
A pointer to an array walks a matrix one row at a time:
#include <stdio.h>
int main(void) {
int m[2][3] = {{1, 2, 3}, {4, 5, 6}};
int (*row)[3] = m; /* points to row 0 */
printf("%d\n", row[0][2]);
row++; /* now points to row 1 */
printf("%d %d\n", (*row)[0], row[0][1]);
return 0;
}
3
4 5
4. Passing a 2D array to a function
When you pass m, the function receives a pointer to the first row. The compiler must know the row length to compute m[i][j] (it needs i × columns + j), so every dimension except the first must be written.
#include <stdio.h>
int total(int m[][3], int rows) { /* same as int (*m)[3] */
int sum = 0;
for (int i = 0; i < rows; i++)
for (int j = 0; j < 3; j++)
sum += m[i][j];
return sum;
}
int main(void) {
int marks[2][3] = {{70, 80, 90}, {60, 75, 85}};
printf("total = %d\n", total(marks, 2));
return 0;
}
total = 460
int m[][] (no column size) does not compile. Since C99 you can also pass the sizes first and use them: int total(int rows, int cols, int m[rows][cols]).
5. A matrix with malloc
When the size is known only at run time, a common pattern is an array of row pointers, each pointing to a row from malloc. This uses a pointer to pointer (int **).
#include <stdio.h>
#include <stdlib.h>
int main(void) {
int rows = 3, cols = 4;
int **a = malloc(rows * sizeof(int *)); /* the row pointers */
for (int i = 0; i < rows; i++)
a[i] = malloc(cols * sizeof(int)); /* each row */
for (int i = 0; i < rows; i++)
for (int j = 0; j < cols; j++)
a[i][j] = i * cols + j;
printf("%d %d\n", a[1][2], a[2][3]);
for (int i = 0; i < rows; i++)
free(a[i]); /* free rows first */
free(a); /* then the pointer array */
printf("freed\n");
return 0;
}
6 11
freed
a[i][j] works here too, but for a different reason: a[i] reads a pointer from the row array, then [j] indexes that row. The rows are not guaranteed to be next to each other in memory, so you cannot treat this as one flat block. Free the rows before the array of pointers — if you free a first, you lose the addresses of the rows (a memory leak).
Common mistakes
- Writing
int *p[3]when you meantint (*p)[3]. Remember:[]binds tighter than*. - Thinking
m + 1moves one int in a 2D array — it moves one row. - Passing an
int m[3][4]to a function that expectsint **. They are different types: the 2D array is one block of ints, whileint **expects an array of pointers. The compiler warns, and the program misbehaves. - Leaving out the column size in a function parameter (
int m[][]). - Freeing the pointer array before the rows.
Quick recap
- A 2D array is stored row-major:
m[i][j]is elementi × cols + j. m[i][j] == *(*(m + i) + j);m + 1moves one row.int *p[3]= array of 3 pointers;int (*p)[3]= pointer to an array of 3 ints.- Arrays of pointers store strings of different lengths without wasting space.
- Functions taking a 2D array must know every dimension except the first.
- Dynamic matrix:
int **plus onemallocper row; free rows, then the array.