C Multidimensional Arrays

A regular (one-dimensional) array stores a list of values — like a single row of boxes. A multidimensional array stores data in multiple rows and columns — like a grid or a table. The most common multidimensional array in C is the two-dimensional (2D) array, which works exactly like a spreadsheet with rows and columns.

Why Use Multidimensional Arrays?

Some real-world data is naturally arranged in a grid:

  • A class of students with marks in multiple subjects
  • A chessboard (8×8 grid)
  • A pixel grid for image processing
  • A matrix in mathematics

A 2D array stores all of this efficiently in one variable.

2D Array — Declaration and Initialization

Syntax

data_type array_name[rows][columns];

Example: 3 rows × 4 columns

int marks[3][4];   // 3 students, 4 subjects each
// Total elements: 3 × 4 = 12

Visual Layout


marks[3][4] — 3 rows, 4 columns:

         Col 0   Col 1   Col 2   Col 3
Row 0  [  72  ] [  85  ] [  90  ] [  78  ]
Row 1  [  65  ] [  70  ] [  88  ] [  92  ]
Row 2  [  55  ] [  60  ] [  75  ] [  80  ]

Access: marks[row][col]
marks[0][2] = 90  (Row 0, Column 2)
marks[2][3] = 80  (Row 2, Column 3)

Initializing a 2D Array

Method 1: Full initialization with braces

int marks[3][4] = {
    {72, 85, 90, 78},   // Row 0 — Student 1
    {65, 70, 88, 92},   // Row 1 — Student 2
    {55, 60, 75, 80}    // Row 2 — Student 3
};

Method 2: Single line (compiler fills rows automatically)

int marks[3][4] = {72, 85, 90, 78, 65, 70, 88, 92, 55, 60, 75, 80};

Method 3: Partial initialization (rest filled with 0)

int grid[3][3] = {{1, 2}, {3}};
// grid[0] = {1, 2, 0}
// grid[1] = {3, 0, 0}
// grid[2] = {0, 0, 0}

Accessing Elements

Each element is accessed using two indices: [row][column]. Both indices start from 0.

#include <stdio.h>

int main()
{
    int marks[3][4] = {
        {72, 85, 90, 78},
        {65, 70, 88, 92},
        {55, 60, 75, 80}
    };

    printf("Student 2, Subject 3: %d\n", marks[1][2]);   // Output: 88
    printf("Student 3, Subject 4: %d\n", marks[2][3]);   // Output: 80

    return 0;
}

Traversing a 2D Array with Nested Loops

A nested for loop (a loop inside another loop) is the standard way to visit every element of a 2D array.

#include <stdio.h>

int main()
{
    int marks[3][4] = {
        {72, 85, 90, 78},
        {65, 70, 88, 92},
        {55, 60, 75, 80}
    };

    printf("Student Marks Table:\n");
    printf("%-10s Sub1  Sub2  Sub3  Sub4\n", "Student");

    for (int i = 0; i < 3; i++)   // loop through rows
    {
        printf("Student%-3d", i + 1);

        for (int j = 0; j < 4; j++)   // loop through columns
        {
            printf("  %3d  ", marks[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Student    Sub1  Sub2  Sub3  Sub4
Student1     72    85    90    78
Student2     65    70    88    92
Student3     55    60    75    80

Real Example: Matrix Addition

#include <stdio.h>

int main()
{
    int A[2][2] = {{1, 2}, {3, 4}};
    int B[2][2] = {{5, 6}, {7, 8}};
    int C[2][2];

    // Add matrices A and B
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            C[i][j] = A[i][j] + B[i][j];
        }
    }

    // Print result matrix C
    printf("Matrix A + B =\n");
    for (int i = 0; i < 2; i++)
    {
        for (int j = 0; j < 2; j++)
        {
            printf("%d ", C[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Matrix A + B =
6 8
10 12

3D Arrays

A three-dimensional array adds a third dimension — like stacking multiple 2D grids on top of each other. Think of it as a book: the first index selects the page, the second selects the row, and the third selects the column.

int cube[2][3][4];
// 2 layers, each layer has 3 rows and 4 columns
// Total elements: 2 × 3 × 4 = 24

Visual: 3D Array as Pages of a Book


cube[2][3][4]:

Page 0:                    Page 1:
[0][0] = a b c d          [1][0] = m n o p
[0][1] = e f g h          [1][1] = q r s t
[0][2] = i j k l          [1][2] = u v w x

Access: cube[page][row][col]
cube[1][2][3] = x  (Page 1, Row 2, Column 3)
#include <stdio.h>

int main()
{
    int nums[2][2][2] = {
        {{1, 2}, {3, 4}},    // Page 0
        {{5, 6}, {7, 8}}     // Page 1
    };

    printf("nums[0][1][1] = %d\n", nums[0][1][1]);   // Output: 4
    printf("nums[1][0][0] = %d\n", nums[1][0][0]);   // Output: 5

    return 0;
}

Passing 2D Arrays to Functions

When passing a 2D array to a function, the number of columns must be specified in the parameter.

#include <stdio.h>

void printMatrix(int arr[][3], int rows)
{
    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < 3; j++)
        {
            printf("%3d ", arr[i][j]);
        }
        printf("\n");
    }
}

int main()
{
    int matrix[2][3] = {{10, 20, 30}, {40, 50, 60}};
    printMatrix(matrix, 2);
    return 0;
}

Output:

 10  20  30
 40  50  60

Memory Layout of 2D Arrays

A 2D array in C is stored in memory in row-major order — all elements of the first row come first, followed by all elements of the second row, and so on.


int a[2][3] = {{1, 2, 3}, {4, 5, 6}};

Memory layout (row-major order):
Address: 1000  1004  1008  1012  1016  1020
Value:    1     2     3     4     5     6
          ^--- Row 0 ---^   ^--- Row 1 ---^

a[1][2] = *(a + 1*3 + 2) = *(a + 5) = value at address 1020 = 6

Summary

Multidimensional arrays in C store data in a grid-like structure with rows and columns. A 2D array uses two indices — [row][column] — and is the most common form. Nested for loops are the standard way to read and write all elements. 2D arrays are ideal for storing tables, matrices, and grids. C stores 2D arrays in row-major order in memory, meaning all elements of one row are stored before the next row begins. 3D arrays extend this concept by adding a third dimension, useful for representing pages of data or 3D structures like cubes.

Leave a Comment

Your email address will not be published. Required fields are marked *