Double Dimensional Arrays in C

Double Dimensional Arrays are the arrays which consists of rows and columns. They represent two dimensional matrices of mathematics. All the operation which can be done on two dimensional matrices can be perfomed on Double Dimensional Arrays or two Dimensional Arrays.
We have to specify the number of rows and columns in the squire brackes when we declare the two dimensional array.

Syntax of two dimensinal array declaration :
dataype arrayname[no_of_rows][no_of_columns];
Example:
int marks[3][2];
float prices[2][3];

Initialization of Array

Array can be initialized by specifying the row index and column index in the squire brackets. If we want to initialize first row and first column element then we have to specify as follows
marks[0][0]=20;
The above statement can be interpreted as first row and first column of marks array is initilizes with 20; Similarly if we want access or display we have to specify the row index and column index of the array which we want to access. Example
printf("%d", marks[1][2]);
The above print statement prints second row and third column of marks array.

Always the array index starts with 0

Program to Demonstrate initialization and display of two dimensional array


Program Output




If we want to initialize array elements in run time we can use scanf function similar to normal variable initialization. But in the scanf function we have to refer the arrayname along with the row index and column index in the squire brackets. The following statement illustrates the initialization of array element in run time.
scanf("%d",marsk[2][2]);
The above scanf statement initializes marks array third row and third column with a values which will be entered in run time. If we want to initialize all the elements of the array at a time in run time then we will use sscanf function in any loop with appropriate loop condition. For example marks array is declared with 3 rows and 2 columns then the scanf statement in the loop is as follows

for(i=0;i<3;i++){ //i for rows
for(j=0;j<2;j++)// j for columns {
scanf("%d",marks[i][j]);
}
}
Similarly if we want display all the elements we can use the following snippet. for(i=0;i<3;i++){ //i for rows
for(j=0;j<2;j++)// j for columns {
printf("%d",marks[i][j]);
}
}

Program to Demonstrate initialization and display of two dimensional array Using runtime initialization


Program Output