Two-Dimensional Array



Two-dimensional array are those type of array, which has finite number of rows and finite number of columns.  The declaration form of two-dimensional array is

Syntax :- Data_type array_name [row size][column size];

The type may be any valid type supported by C. The rule for giving the array name is same as the ordinary variable. The row size and column size should be an individual constant. The following declares a two-dimensional 3 by 3 array of integers and sets the first and last elements to be 10.

E.g.: int matrix [3][3];
matrix[0][0] = 10;
matrix[2][2] = 10;

The following Figure illustrates a two dimensional array, matrix. The array contains three rows and three columns, so it is said to be a 3-by-3 array. In general, an array with m rows and n columns is called an m-by-n array.

[0] [1] [2]
[0] [10]
[1]
[2] [10]

Every element in array matrix is identified by an element name of the form matrix[ i ][ j ]; matrix is the name of the array, and i and j are the subscripts that uniquely identify each element in matrix . Notice that the names of the elements in the first row all has a first subscript of 0; the names of the elements in the third column all have a second subscript of 2.

In the case of Two-dimensional array, during declaration the maximum number of rows and maximum number of column should be specified for processing all array elements.

Initialization of two-dimensional array:
two dimensional array can be initialized as follows:
E.g.: i) int arr [2][3]={{1,2,3},{4,5,6}};

The above statement means there will be two rows and three columns. The one pair of brace brackets represents values for on row.


E.g.: ii) int arr [][3]={{9, 17,15}{8, 6, 3}};

In this we don’t have to write the number of rows. By initializing an array automatically the memory of two rows and three columns will get reserved.