Types of Arrays in C

In C programming, arrays are used to store multiple values in a single variable. An array is a collection of elements, all of the same data type, stored in contiguous memory locations.


📌 1. Single-Dimensional Array

A 1D array is a list of elements arranged in a single row.

Syntax:

data_type array_name[size];

Example:

int marks[5] = {90, 85, 78, 92, 88};

Diagram:

Index: 0 1 2 3 4 +-----+-----+-----+-----+-----+ marks → | 90 | 85 | 78 | 92 | 88 | +-----+-----+-----+-----+-----+

Accessing:

Usage:


📌 2. Two-Dimensional Array

A 2D array is a table or matrix of elements. It uses two indices: row and column.

Syntax:

data_type array_name[rows][columns];

Example:

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

Diagram:

Column → 0 1 2 +---+---+---+ Row 0 | 1 | 2 | 3 | +---+---+---+ Row 1 | 4 | 5 | 6 | +---+---+---+

Accessing:

How to access Elements

Usage:


📌 3. Multi-Dimensional Array

A Multi-Dimensional Array is an extension of 2D arrays and can go up to any number of dimensions (like 3D).

Syntax:

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

Example (3D Array):

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

Accessing:

3D Array in C: int cube[2][2][2]

This declaration creates a 3D array with:

Total elements = 2 × 2 × 2 = 8

Block 0 (cube[0])

cube[0][0][0] = 1
cube[0][0][1] = 2
cube[0][1][0] = 3
cube[0][1][1] = 4

Block 1 (cube[1])

cube[1][0][0] = 5
cube[1][0][1] = 6
cube[1][1][0] = 7
cube[1][1][1] = 8

Tip: Think of it as a cube of data like cube[block][row][column]

Usage:


✅ Summary Table

Note: The more dimensions, the more memory and complexity involved. Start with 1D or 2D for most beginner use cases.