×
☰ Menu

Multidimensional Arrays in C

In simple terms, multidimensional arrays can be defined as an array of arrays. Tables are used to store data in multidimensional arrays. Arrays in three dimensions are simple to visualize. However, we may have trouble envisioning a four-, five-, or n-dimensional array in general.

The general form of declaring N-dimensional arrays:

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

data_type: Type of data to be stored in the array. data_type is any valid C data type (int, float, char, etc. )
array_name: Name of the array
size1, size2,... ,sizeN: Sizes of the dimensions


Size of multidimensional arrays

Multiplying the sizes of all the dimensions provides the total number of elements that can be stored in a multidimensional array.

For example:

The array int a[3][3] can store total (3*3) = 9 elements.

Similarly array int a[3][3][3] can store total (3*3*3) = 27 elements.

 

Initializing Three-Dimensional Array:

Three-dimensional arrays have the same initialization as two-dimensional arrays. The distinction is that as the number of dimensions grows, so does the number of nested braces.

Method 1:

int x[2][3][4] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23};

Method 2: 

int a[2][3][4] =
{
{ {0,1,2,3}, {4,5,6,7}, {8,9,10,11} },
{ {12,13,14,15}, {16,17,18,19}, {20,21,22,23} }
};

 

We may create arrays with any number of dimensions in the same way. However, as the number of dimensions grows, so does the complexity. The Two-Dimensional Array is the most often used multidimensional array.