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

  1. 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

  1. 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:

array


An example of a program using 2D array:

Example:

  1. #include <stdio.h>
  2. int main() {
  3. int a[3][2] = { {7,5}, {3,8}, {0,9} };
  4. int i, j;
  5. for(i = 0; i < 3; i++) {
  6. for(j = 0; j < 2; j++) {
  7. printf("a[%d][%d] = %d\n", i,j, a[i][j] );
  8. }
  9. }
  10. return 0;
  11. }

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