C programming multidimensional arrays

Along with one-dimensional array, c programming also allows multidimensional arrays. In this tutorial, you will learn about c programming multidimensional arrays.

c programming multidimensional arrays

C programming multidimensional arrays

In one-dimensional arrays, elements are arranged in one direction but in multidimensional array data items are arranged in multiple directions.

Like in two-dimensional array data items are arranged in two directions i.e rows and columns. Data items arranged in the horizontal direction are called rows and that in vertical directions are referred as columns.

Moreover, the following picture explains the concept of a  C programming multidimensional array.



An array is a sequenced collection of similar kinds of data items that can be represented by single variable name. The individual data item in an array is called element.

Initializing a two-dimensional array

How do we initialize a two-dimensional array?

data_type array_name[row_size][columns_size];  //declaring two dimensional array


int a[4][2] = {
               {1234, 56}
               {1321, 22}
               {6545, 44}
               {3412, 31}
int a[4][2] = {1234, 56, 1321, 22, 6545, 44, 3412, 31};

Memory map of a two-dimensional array

memory map

Initializing  a multidimensional array

data_type array_name[s1][s2][s3]....[sn];;  //declaring multi dimensional array


int a[3][3][2] = {

C multidimensional array

Common Programming Error
Defining double array elements as a[x,y] instead of a[x][y] is programmer error rather than syntax error because the comma is treated as an operator.


Example: Initializing C programming multidimensional arrays.

#include <stdio.h>
void showArray(int x[][3]);  //function prototype
int main ()
   int a[2][3] = {{1,2,3}, {4,5,6}};   //initializing array
   int b[2][3] = {1, 2, 3, 4, 5};
   int c[2][3] = {{1, 2}, {4}};
   printf("values in array a by row:\n");
   printf("values in array b by row:\n");

   printf("values in array c by row:\n");
   return 0;
}             // end of main
void showArray(int x[][3])
   int i;      //row counter
   int j;      //column counter
   for (i = 0; i <= 1; ++i)
      for (j = 0; j<= 2; ++j)
         printf("%d", x[i][j]);
      }       //end of inner for
   }          //end of outer for
}             //end of function


values in array a by row:
values in array b by row:
values in array c by row:

There are three arrays in this program. The elements of an array that are not initialized explicitly are initialized zero, as in the second and third array of the above program.

The subscripts of every array are used by the compiler to determine the location of array elements in the memory.