Two-Dimensional Arrays


C supports multidimensional arrays. The simplest form of a multidimensional array is a two-dimensional array. A two-dimensional array is basically an array of one-dimensional arrays. You write to declare a two-dimensional integer array of size 20,20.

int a[5][10];

Sample Program

Program
#include <stdio.h>

int main() {
  int numbers[3][3];
  
  int count=1;
  for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
   {
    numbers[i][j]=count;
    ++count;
   }
  }
  
  /* print them */
  for(int i=0;i<3;i++)
  {
   for(int j=0;j<3;j++)
   {
    printf("%3d",numbers[i][j]);
   }
   printf("\n");
  }
  return 0;
}

In this example, numbers[0][0] has the value 1, numbers[0][1] the value 2, numbers[0][2] the value 3, and so on. The value of numbers[2][2] will be 9. You can visualize the numbers arrays as shown here:

Two-Dimensional Arrays

Two-dimensional arrays are stored in a row-column matrix, where the first index indicates the row and the second indicates the column.


When a two-dimensional array is used as an argument for a function, only the pointer to the first element is actually passed. However, the parameter obtained in a two-dimensional array must determine the minimum dimension size. The right dimension is needed because the compiler needs to know the length of each row if it accurately indicates the array. For example, activity takes a two-dimensional integer array with dimensions 10,10:

void func1(int x[][10])
{
 ....
}