⏱ 1 min read

Multidimensional Arrays & Pointers

C stores multidimensional arrays in row-major order.

Example:

int B[2][3];

Meaning:

  • array of 2 rows
  • each row has 3 ints

So type of B is:

int (*)[3]

Correct pointer for 2D array

Wrong:

int *p = B; // ❌

Correct:

int (*p)[3] = B; // ✅

Address arithmetic

Assume base address B=400
Row size = 3 * 4 = 12 bytes

  • B → 400
  • B+1 → 412

Access formula

B[i][j] == *(*(B+i) + j)

Passing 2D array to function

Must specify column size:

void f(int B[][3], int r);

or

void f(int (*B)[3], int r);

3D arrays

int arr[2][2][3];
int (*p)[2][3] = arr;

Dimensions after first must match.