C Multi-Dimensional Array

A multi-dimensional array uses two or more indexes to organize same-type elements across multiple dimensions.

A two-dimensional array has rows and columns. A three-dimensional array adds another dimension, which you can imagine as layers. If you need the simpler form first, review Two Dimensional Array.

What Is a Multi-Dimensional Array in C?

A multi-dimensional array is an array with more than one dimension.

A one-dimensional array uses one index. A two-dimensional array uses two indexes. A three-dimensional array uses three indexes.

All elements still have the same type. The extra dimensions only change how the elements are organized and accessed.

C Multi-Dimensional Array Syntax

A three-dimensional array can be declared like this:

data_type array_name[size1][size2][size3];

For example:

int data[2][2][3];

This creates a three-dimensional array with 2 × 2 × 3 = 12 integer elements.

Three-dimensional C array shown as layers, rows, and columns A layered cube-style diagram showing two layers of a two-by-three integer array. Each layer has two rows and three columns. The element data one zero two is highlighted as value nine, and three nested loops are shown for traversal. THREE DIMENSIONS = LAYER + ROW + COLUMN Each extra dimension adds another index int data[2][2][3]; LAYER 0 1 2 3 4 5 6 LAYER 1 7 8 9 10 11 12 row 0 row 1 col 0 col 1 col 2 data[1][0][2] layer 1 row 0 column 2 → 9 INDEX ORDER data[layer][row][column] TRAVERSE ALL ELEMENTS loop 1 → layers loop 2 → rows • loop 3 → columns A multi-dimensional array is still one C array type; the extra brackets describe nested array dimensions.

C Multi-Dimensional Array Example

This program creates a 2 × 2 × 3 array and uses three nested loops to print every element.

#include <stdio.h>

int main(void) {
    int data[2][2][3] = {
        {
            {1, 2, 3},
            {4, 5, 6}
        },
        {
            {7, 8, 9},
            {10, 11, 12}
        }
    };

    int layer;
    int row;
    int column;

    for (layer = 0; layer < 2; layer++) {
        printf("Layer %d:\n", layer);

        for (row = 0; row < 2; row++) {
            for (column = 0; column < 3; column++) {
                printf("%d ", data[layer][row][column]);
            }

            printf("\n");
        }
    }

    return 0;
}

Output

Layer 0:
1 2 3
4 5 6
Layer 1:
7 8 9
10 11 12

The first loop selects a layer. The second loop selects a row inside that layer. The third loop selects a column inside that row.

Initializing a Multi-Dimensional Array

Braces can be nested to match the dimensions:

int data[2][2][3] = {
    {
        {1, 2, 3},
        {4, 5, 6}
    },
    {
        {7, 8, 9},
        {10, 11, 12}
    }
};

The outer groups represent the first dimension. Inside them are groups for the next dimensions.

Accessing a Multi-Dimensional Array Element

Use one index for each dimension:

data[1][0][2]

In the example, this selects the value 9. Using a layer-row-column view, the indexes are layer 1, row 0, and column 2.

Changing an Element

You can assign a new value by using all required indexes:

data[0][1][2] = 99;

This changes only the selected element.

Traversing a Multi-Dimensional Array

A three-dimensional array is commonly traversed with three nested loops.

Each loop controls one dimension:

for (layer = 0; layer < 2; layer++) {
    for (row = 0; row < 2; row++) {
        for (column = 0; column < 3; column++) {
            printf("%d ", data[layer][row][column]);
        }
    }
}

Valid Index Ranges

For int data[2][2][3], the first index can be 0 or 1.

The second index can also be 0 or 1. The third index can be 0, 1, or 2.

Using an index outside a valid range causes undefined behavior.

Common Multi-Dimensional Array Mistakes

Common mistakes when using multi-dimensional arrays in C
Mistake Correct Idea
Using too few indexes when trying to select one scalar element. Use one index for each dimension, such as data[1][0][2].
Using an invalid index such as data[2][0][0] when the first size is 2. The valid first indexes are 0 and 1.
Using only one or two loops to visit every element of a three-dimensional array directly. Three nested loops are the common beginner pattern for three dimensions.

Short Practice

  1. Declare a three-dimensional integer array named box with sizes 2, 3, and 4.
  2. How many integer elements are in int box[2][3][4]?
  3. In data[1][0][2], identify the first, second, and third index values.

FAQs

What is a multi-dimensional array in C?

A multi-dimensional array in C is an array that uses two or more indexes to organize same-type elements across multiple dimensions.

How do you declare a three-dimensional array in C?

Use three bracketed sizes, such as int data[2][2][3];.

What does data[1][0][2] mean in C?

It selects the element at first index 1, second index 0, and third index 2. In a layer-row-column view, that can be read as layer 1, row 0, column 2.

How many loops are commonly used to traverse a three-dimensional array?

Three nested loops are commonly used: one for the first dimension, one for the second, and one for the third.

Summary

A multi-dimensional C array uses two or more indexes to organize same-type elements. A three-dimensional array uses three indexes, which can be imagined as layer, row, and column positions. You can initialize, access, update, and traverse the elements with one index or loop for each dimension.