Multi-dimensional Arrays in C

Learn two-dimensional arrays, nested element access, and the extension to three or more dimensions. Complete the array basics lesson first. Array basics →

Multidimensional arrays: tables and layers

A one-dimensional array is a line of elements; a two-dimensional array is a table of rows and columns . Type name[rows][columns]; declares the array.

A two-dimensional array: 3 rows × 4 columns

int a[3][4];   // 3 rows × 4 columns = 12 int elements 
1
2
3
4
5
6
7
8
9
10
11
12
Blue: row 0; green: row 1 (orange: a[1][2]); red: row 2
a[row][column] is the access order; both indices start at zero . a[1][2] means the second row, third column: 7.

Visit every element with nested for loops

for (int i = 0; i < 3; i++)        // Outer loop: rows 
    for (int j = 0; j < 4; j++)    // Inner loop: columns 
        printf("%d ", a[i][j]);
⚠️ Swapping i and j accesses a different element . a[j][i] interchanges the row and column indices. Keep the outer loop for rows and the inner loop for columns to avoid confusion.

Three or more dimensions: add another index

int cube[2][3][4];   // 2 layers × 3 rows × 4 columns 
cube[Layer ][Row ][Column ]

// Use three nested loops to visit all elements: 
for (int l = 0; l < 2; l++)
  for (int i = 0; i < 3; i++)
    for (int j = 0; j < 4; j++)
        cube[l][i][j] = 0;
For each extra dimension, add an index and a nested loop . The same idea extends to four or five dimensions, although two or three are more common. The next section covers declaration and initialization.

Declaring and initializing 2D and 3D arrays

Two-dimensional array

// 3 rows × 4 columns 
int m[3][4] = {
    { 1,  2,  3,  4},
    { 5,  6,  7,  8},
    { 9, 10, 11, 12}
};

printf("%d\n", m[1][2]);  // → 7 (row 1, column 2, zero-based) 

Three-dimensional array

// 2 layers × 3 rows × 4 columns 
int cube[2][3][4];

for (int l = 0; l < 2; l++)
    for (int i = 0; i < 3; i++)
        for (int j = 0; j < 4; j++)
            cube[l][i][j] = l * 100 + i * 10 + j;
Initialization shorthand: int m[3][4] = {0}; initializes all elements to zero. In int m[][4] = {{1,2,3,4},{5,6,7,8}};, only the outermost dimension is omitted; the column count is still required.

Checking sizeof

int m[3][4];
sizeof(m)        // 48 (3 * 4 * 4 bytes) 
sizeof(m[0])     // 16 (one row = 4 * 4 bytes) 
sizeof(m[0][0])  // 4 (one int) 
// Idioms for element counts: 
// Rows = sizeof(m) / sizeof(m[0]) 
// Columns = sizeof(m[0]) / sizeof(m[0][0]) 

Try it yourself: multidimensional arrays

Declare two- and three-dimensional arrays and process them with nested for loops.
Task 1: Multiplication table
int table[9][9]: fill this array with the 1–9 multiplication table and print it. table[i][j] = (i+1) * (j+1) is the assignment to perform in nested loops.
Task 2: Row totals
Initialize int a[3][4] with values of your choice. Calculate and print the sum of each row, using an outer row loop and an inner column loop.
Task 3: Transpose rows and columns
Given int A[3][4], create int B[4][3]. Transpose with B[j][i] = A[i][j] and print the result.

Lessons to revisit

Related Lessons

Basics
Lesson 20: Arrays
Review array indexing, initialization and out-of-bounds access.
Advanced
Lesson 36: Pointer Basics
Solidify your understanding of arrays, pointers and address arithmetic.
Advanced
Lesson 42: Dynamic Memory (malloc/free)
Review the basics of safely allocating and freeing arrays whose size is decided at run time.
Practice problems for this lessonArrays: 13 practice problemsWrite code in your browser and get it checked automatically. Hints and solutions included →
← Previous lesson
Lesson 24: Quiz (Strings)
Next lesson →
Lesson 26: Functions (Basics)

Review Quiz

Check your understanding of this lesson!

Q1. In int a[3][4], which position does a[1][2] access?

Second row, third column (both indices start at zero)
First row, second column
Third row, fourth column
Both indices in a[row][column] start at zero. a[1] selects the second row, and [2] selects its third column.

Q2. Which loop visits every element of int a[3][4]?

for(i=0;i<3;i++) for(j=0;j<4;j++) a[i][j]
for(i=0;i<4;i++) for(j=0;j<3;j++) a[i][j]
for(i=0;i<7;i++) a[i]
The outer loop visits rows 0–2; the inner loop visits columns 0–3. Do not interchange row and column bounds. Swapping i and j accesses a different element.

Q3. How many elements does int cube[2][3][4] contain?

24 (2 × 3 × 4)
9 (2 + 3 + 4)
12
Multiply the lengths of all dimensions: 2 × 3 × 4 = 24 int elements.