Multi-Dimensional Array in C

C does not have true multidimensional arrays. However, because of the generality of C’s type system; we can have arrays of arrays. This is what a 2D Array is. Consider the example of 2D Array discussed in last article.

Screenshot from 2020-07-20 10-35-28

In this case, each row is one-dimensional array of 2 elements. Now, all these rows are treated as single variables and are placed next to each other. This gives rise to one dimensional Array of one-dimensional Arrays. In other words, we have a multi-dimensional array.

C allows array of two or more dimensions and maximum number of dimensions an Array in a C program can have depends upon the compiler. Logically, there is no limit on the maximum number of dimensions. So in C programming an array can have two or three or four or even ten or more dimensions. More dimensions in an array means more data it can hold and of course more difficulties to manage and understand these arrays. A multidimensional array has following syntax:

<data-type> <Array-Name> [d1][d2][d3]…[dn];

Where d i is the size of i th dimension.

Consider a 3D array declared and initialized as follows:

int array_3d[3][3][3]=
{
{
{11, 12, 13},
{14, 15, 16},
{17, 18, 19}
},
{
{21, 22, 23},
{24, 25, 26},
{27, 28, 29}
},
{
{31, 32, 33},
{34, 35, 36},
{37, 38, 39}
},
};

This means the array_3d is one dimensional array of 2D arrays. On other words, each element of this array is a 2D array. The pictorial representation of array_3d is as follows.

Screenshot from 2020-07-20 10-38-37


For any query or issue, feel free to discuss on http://discuss.eduguru.in