Initialisation of 2-Dimensional array

Initialisation of 2-Dimensional array:-

After declaring array, the array elements are initialised with random values. Hence we explicitly initialise the array elements with meaningful values. Following are the different methods in which array elements can be initialised,

Example:

Method 1 :

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

Method 2 :

int a[2][3] = { 1,2,3,4,5,6};

Method 3 :

int a[2][3] = {
                       {1,2},
                       {4}
                      }

In method 3 values for selected elements are initialised and values for remaining element are not provided and they will be taken as zero by default.

Method 4:

int a[2][3] = { 0 };

In this case values for all array elements will be zero.

Comments