Multidimensional array in c

An array with more than one index value is called a Multidimensional array in C or other programming language. Array can be single, two, three, up to n-dimension.

But most of programmers only used up to three dimension array in their codes.

All the array below is called a single-dimensional array.

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

Two dimensional array is

int list[2][2] = {{1,12},{13,20}};

OR

int list[2][2] = {
                  {2,3},
                  {3,2}
                 };

the complex one three dimensional array is

int list[2][2][2] = { 
                     {
                       {2,4}
                     },
                     {
                       {4,2}
                     }
                    };
OR

int list[2][2][2] = { { {2,4} },{ {2,4} } };

Declaration of Multidimensional Array in C

To declare a multidimensional array you can do follow the syntax

data_type array_name[size][size][size];

The number of square brackets specifies the dimension of the array.

For example to declare two dimensions integer array we can do as follows:

int matrix[3][3]; 

Initializing Multidimensional Array

You can initialize an array as a single-dimension array. Here is an example of initializing a two dimensions integer array:

int matrix[3][3] =  {  {11,12,13},  {21,22,23}, {32,31,33} };
Or
int matrix[3][3] =  {11,12,13,21,22,23,32,31,33 };

Recommended Post: