C Two-dimensional Arrays
C language allows multi-dimensional arrays (two-dimensional and three-dimensional arrays). Multi-dimensional arrays means array of arrays.
Two-dimensional arrays are declared as follows:
Syntax
type array_name [row_size][column_size];
Two-dimensional arrays are the simplest form of multidimensional array. An example of two-dimensional array is given below:
Example
int arr [3][4];
This is an example of a two-dimensional array which contains 3 rows and 4 columns. The representation of the two-dimensional array is following:
An example of a program using 2D array:
Example:
#include <stdio.h>
int main() {
int a[3][2] = { {7,5}, {3,8}, {0,9} };
int i, j;
for(i = 0; i < 3; i++) {
for(j = 0; j < 2; j++) {
printf("a[%d][%d] = %d\n", i,j, a[i][j] );
}
}
return 0;
}
Output:
a[0][0] = 7 a[0][1] = 5 a[1][0] = 3 a[1][1] = 8 a[2][0] = 0 a[2][1] = 9