Two Dimensional Arrays

C also supports multidimensional arrays and the simplest form of the multidimesional array is two-dimensional array. The general form of declaration of the two dimensional array would be

type arrayname[x][y];

Two-d arrays are stored in a row-column martix , in which 

To access a specific element, both the indices or subscripts have to be specified.

 

Initialising Two Dimensional Arrays

The rules for initialisting a two dimensional are the same as for a one dimensional array. Initialisation at the time of declaration must be done outside the function or must be declared static within the function.

Consider the following example:

int squares[10][2] = {

                                {1,1},

                                {2,2},

                                {3,3},

{4,4},

{5,5},

{6,6},

{7,7},

{8,8},

{9,9},

{0,0}

}

The row subscript may be left blank for a more flexible declaration. The compiler will automatically calculate the row dimension based on the number of values initialised .

The inner sets of curly braces are optional. However, in the case of initialisting the array with only some and not all values, these curly braces assume a lot of importance.

For instance, in the declaration

int s[3][4] = {

                     {143,274},

                     {336,542,679},

                     {442,419,765,96}

                    }

only two elements of the first row, three elements in the second row and four in the third row are initialised.

 

 

Copyright © 2008 Manikkumar. All rights reserved.