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.

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

Example:

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

Example:

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

[adsense1]
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");
   showArray(a);
   
   printf("values in array b by row:\n");
   showArray(b);

   printf("values in array c by row:\n");
   showArray(c);
   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
      printf("\n");
   }          //end of outer for
}             //end of function

Output

values in array a by row:
123
456
values in array b by row:
123
450
values in array c by row:
120
400

Explanation
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.