Home » Data Structure » One Dimensional and Multidimensional Array with Example

One Dimensional and Multidimensional Array with Example

Last updated:

Support this post with a reaction:

3,147 views

An array in C is a linear data structure that stores multiple elements of the same data type under a single variable name. Each element is stored in a continuous block of memory and accessed using an index. Based on the number of dimensions, arrays are classified into one-dimensional arrays and multidimensional arrays. A one-dimensional array stores elements in a linear sequence, whereas a multidimensional array organizes elements into rows, columns, or multiple layers. In this article, you will learn the types of arrays in C, their declaration, initialization, memory representation, examples, programs, and differences.

Basic Array Declaration

What Is an Array in C?

An array in C is a collection of elements of the same data type stored in contiguous memory locations. Instead of declaring separate variables for multiple values, we can store them using one array name and access individual elements through their indexes.

Example:

int numbers[5] = {10, 20, 30, 40, 50};

Here:

  • int represents the data type.
  • numbers is the array name.
  • 5 represents the number of elements.
  • 10, 20, 30, 40, 50 are the array elements.

The first element is accessed using index 0, and the last element is accessed using index 4.

What Are the Types of Arrays in C?

Arrays in C are classified according to the number of dimensions used to organize their elements.

The main types of arrays are:

  1. One-Dimensional Array (1D): Stores elements in a single linear sequence.
  2. Two-Dimensional Array (2D): Stores elements in rows and columns.
  3. Three-Dimensional Array (3D): Organizes elements into multiple layers, rows, and columns.
  4. Higher-Dimensional Array: Uses more than three dimensions to represent complex data.

What Is a One-Dimensional Array in C?

A one-dimensional array is a collection of elements arranged in a single sequence. Each element is identified using one index, which starts from zero in C.

It is commonly used to store marks, salaries, temperatures, ages, and other lists of similar values.

Syntax of a One-Dimensional Array:

data_type array_name[array_size];

Example:

int marks[5];

This statement declares an integer array named marks that can store five integer values.

One-Dimensional Array

How Does a One-Dimensional Array Work?

Consider the following array:

int numbers[5] = {10, 20, 30, 40, 50};

Its representation is:

Index01234
Element1020304050

Each element can be accessed using its index.

For example:

  • numbers[0] returns 10.
  • numbers[1] returns 20.
  • numbers[2] returns 30.
  • numbers[3] returns 40.
  • numbers[4] returns 50.

How to Initialize a One-Dimensional Array in C?

There are two common ways to initialize a one-dimensional array.

Method 1: Initialize elements individually

#include <stdio.h>

int main() {
    int numbers[5];

    numbers[0] = 10;
    numbers[1] = 20;
    numbers[2] = 30;
    numbers[3] = 40;
    numbers[4] = 50;

    return 0;
}

Method 2: Initialize using an initializer list

#include <stdio.h>

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};

    return 0;
}

When an array is partially initialized using an initializer list, the remaining elements are automatically initialized to zero.

Example:

int numbers[5] = {10, 20};

The resulting elements are 10, 20, 0, 0, 0.

C Program to Display One-Dimensional Array Elements

#include <stdio.h>

int main() {
    int numbers[5] = {10, 20, 30, 40, 50};
    int i;

    printf("Array elements are:\n");

    for (i = 0; i < 5; i++) {
        printf("%d ", numbers[i]);
    }

    return 0;
}

Output:

Array elements are:
10 20 30 40 50

Explanation of the Program:

  1. Declare an integer array named numbers with five elements.
  2. Initialize the array with the values 10, 20, 30, 40, and 50.
  3. Declare a loop variable i to access each element.
  4. Use a for loop starting from index 0.
  5. Continue the loop until i becomes less than 5.
  6. Access each array element using numbers[i].
  7. Print the elements one by one.

Characteristics of a One-Dimensional Array

  • Single-dimensional structure: Elements are arranged in a linear sequence.
  • Same data type: All elements have the same declared data type.
  • Zero-based indexing: The first element is stored at index 0.
  • Contiguous memory: Array elements occupy consecutive memory locations.
  • Direct access: Elements can be accessed using their indexes in O(1) time.
  • Fixed declared size: The size of an ordinary fixed-size array cannot be changed after its creation.

What Is a Multidimensional Array in C?

A multidimensional array is an array that uses two or more dimensions to organize data. It can represent information in the form of rows, columns, and layers.

In C, a multidimensional array is an array whose elements are themselves arrays.

For example, a two-dimensional array represents a table, while a three-dimensional array can represent multiple tables or layers.

Syntax of a Multidimensional Array:

data_type array_name[size1][size2]...[sizeN];

Here:

  • data_type represents the type of elements.
  • array_name represents the array identifier.
  • size1, size2, ... represent the dimensions of the array.

The two most commonly used multidimensional arrays are 2D and 3D arrays.

What Is a Two-Dimensional Array in C?

A two-dimensional array is an array arranged in rows and columns. It is also called a matrix because its structure is similar to a mathematical matrix or spreadsheet.

Each element is accessed using two indexes: one for the row and another for the column.

Syntax:

data_type array_name[rows][columns];

Example:

int matrix[3][3];

This declaration creates a two-dimensional integer array with three rows and three columns. It can store nine integer elements.

Two-Dimensional Array

How Does a Two-Dimensional Array Work?

Consider the following array:

int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Its representation is:

Row / ColumnColumn 0Column 1Column 2
Row 0123
Row 1456
Row 2789

For example:

  • matrix[0][0] returns 1.
  • matrix[0][1] returns 2.
  • matrix[1][0] returns 4.
  • matrix[2][2] returns 9.

The first index represents the row, and the second index represents the column.

How to Declare and Initialize a Two-Dimensional Array?

Method 1: Declare an array without initialization

int matrix[3][3];

Method 2: Initialize using nested braces

int matrix[3][3] = {
    {1, 2, 3},
    {4, 5, 6},
    {7, 8, 9}
};

Method 3: Initialize using a single list

int matrix[3][3] = {
    1, 2, 3, 4, 5, 6, 7, 8, 9
};

All three initialization approaches represent the same 3 × 3 array.

C Program to Display Two-Dimensional Array Elements

#include <stdio.h>

int main() {
    int matrix[3][3] = {
        {1, 2, 3},
        {4, 5, 6},
        {7, 8, 9}
    };

    int i, j;

    printf("Two-dimensional array elements:\n");

    for (i = 0; i < 3; i++) {
        for (j = 0; j < 3; j++) {
            printf("%d ", matrix[i][j]);
        }
        printf("\n");
    }

    return 0;
}

Output:

Two-dimensional array elements:
1 2 3
4 5 6
7 8 9

Explanation of the Program:

  1. Declare a two-dimensional integer array with three rows and three columns.
  2. Initialize the array with nine integer values.
  3. Declare two loop variables, i and j.
  4. Use the outer for loop to access each row.
  5. Use the inner for loop to access each column.
  6. Access each element using matrix[i][j].
  7. Print each element followed by a space.
  8. Print a new line after completing each row.

What Are the Characteristics of a Two-Dimensional Array?

  • Tabular representation: Elements are organized into rows and columns.
  • Two indexes: Each element is accessed using a row index and a column index.
  • Contiguous storage: C stores the elements in row-major order.
  • Total elements: The number of elements is rows × columns.
  • Applications: Used in matrices, tables, game boards, and image processing.

What Is a Three-Dimensional Array in C?

A three-dimensional array is an array that contains multiple two-dimensional arrays. It organizes elements using three indexes: depth or layer, row, and column.

It is useful when data needs to be represented in multiple layers.

Syntax:

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

Example:

int cube[2][3][4];

This declaration creates a three-dimensional integer array with:

  • 2 layers.
  • 3 rows in each layer.
  • 4 columns in each row.
  • 24 total elements.

How Does a Three-Dimensional Array Work?

Consider the following example:

int cube[2][2][2] = {
    {
        {1, 2},
        {3, 4}
    },
    {
        {5, 6},
        {7, 8}
    }
};

This array contains two layers, with each layer containing two rows and two columns.

An element can be accessed using three indexes.

For example:

  • cube[0][0][0] returns 1.
  • cube[0][1][1] returns 4.
  • cube[1][0][0] returns 5.
  • cube[1][1][1] returns 8.

How to Declare and Initialize a Three-Dimensional Array?

Declaration:

int cube[2][2][2];

Initialization:

int cube[2][2][2] = {
    {
        {1, 2},
        {3, 4}
    },
    {
        {5, 6},
        {7, 8}
    }
};

C Program to Display Three-Dimensional Array Elements

#include <stdio.h>

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

    int i, j, k;

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

        for (j = 0; j < 2; j++) {
            for (k = 0; k < 2; k++) {
                printf("%d ", cube[i][j][k]);
            }
            printf("\n");
        }
    }

    return 0;
}
Layer 0:
1 2
3 4
Layer 1:
5 6
7 8

Explanation of the Program:

  1. Declare and initialize a 3D array named cube.
  2. Declare three loop variables: i, j, and k.
  3. Use the first loop to access each layer.
  4. Use the second loop to access rows within each layer.
  5. Use the third loop to access columns within each row.
  6. Access elements using cube[i][j][k].
  7. Print each layer separately.

What Are the Characteristics of a Three-Dimensional Array?

  • Three-dimensional structure: Elements are organized into layers, rows, and columns.
  • Three indexes: Each element requires three indexes for access.
  • Contiguous memory: Elements are stored in contiguous memory locations in C.
  • Total elements: Layers × rows × columns.
  • Applications: Used in scientific simulations, volumetric data, and image or video processing.