# Pointers and 2D Arrays in C — Array of Pointers, Pointer to Array, Dynamic Matrices

Source: https://skills101.in/learn/c-programming/c-pointers-and-2d-arrays — Skills101 free tutorial (C Programming). Updated 2026-11-18.

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:

```c
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.

![A 3 by 4 matrix drawn as a grid, and the same 12 values laid out in one row in memory: row 0, then row 1, then row 2; the element in row 1, column 2 is at position 1 × 4 + 2 = 6](https://skills101.in/learn-img/c-programming/row-major.svg)

```c
#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;
}
```

```text
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.

- `m` in an expression becomes a pointer to the first **row**. Its type is `int (*)[4]` — "pointer to an array of 4 ints".
- `m + 1` jumps a **whole row** (16 bytes), not one int.
- `*(m + i)` is row `i`, which then becomes a pointer to its first int.
- `*(m + i) + j` points to column `j` of that row.

So `m[i][j]` is exactly `*(*(m + i) + j)`.

```c
#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;
}
```

```text
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 |

![Left: an array of 3 pointers is three separate pointer boxes, each pointing to its own row of a different length. Right: a pointer to an array of 3 ints is a single pointer to a whole row of 3 ints, and p + 1 jumps to the next row](https://skills101.in/learn-img/c-programming/ptr-array-vs-array-ptr.svg)

An **array of pointers** is perfect for a list of names, because each name can have a different length:

```c
#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;
}
```

```text
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:

```c
#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;
}
```

```text
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.

```c
#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;
}
```

```text
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 **`).

```c
#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;
}
```

```text
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 meant `int (*p)[3]`. Remember: `[]` binds tighter than `*`.
- Thinking `m + 1` moves one int in a 2D array — it moves one **row**.
- Passing an `int m[3][4]` to a function that expects `int **`. They are different types: the 2D array is one block of ints, while `int **` 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 element `i × cols + j`.
- `m[i][j] == *(*(m + i) + j)`; `m + 1` moves 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 one `malloc` per row; free rows, then the array.


Practice quiz and free certificate: https://skills101.in/learn/c-programming/c-pointers-and-2d-arrays