Two Dimensional Array in C

The two dimensional array in C language is represented in the form of rows and columns, also known as matrix. It is also known as array of arrays or list of arrays.
The two dimensional, three dimensional or other dimensional arrays are also known as multidimensional arrays.

Declaration of two dimensional Array in C

We can declare an array in the following way.
1. data_type array_name[size1] [size2] ;
A simple example to declare two dimensional array is given below.
1. int twodimen[4] [5] ;
Here, 4 is the row number and 5 is the column number.

Initialization of 2D Array in C

A way to initialize the two dimensional array at the time of declaration is given below.
int arr[4][4] = {{2,3,4,5}, {6,7,8,9}, {10,11,12,13}, {14,15,16,17}} ;

Two dimensional array example in C

#include <stdio.h>  
void main(){    
int i=0,j=0;  
int arr[3][3]={{10,11,12}, {12,14,16}, {18,20,22}};   //traversing 2D array  
for(i=0;i<3;i++) {  
for(j=0;j<3;j++) {  
printf("arr[%d] [%d] = %d  \n", i, j, arr[i] [j]) ;  
}   //end of j  
}   //end of i      
}
Output:
arr[0][0] = 10
arr[0][1] = 11
arr[0][2] = 12
arr[1][0] = 12
arr[1][1] = 14
arr[1][2] = 16
arr[2][0] = 18
arr[2][1] = 20
arr[2][2] = 22

Passing Array to Function in C

To reuse the array operation, we can create functions that receives array as argument. To pass array in function, we need to write the array name only in the function call.
functionname(arrayname); //passing array
There are 3 ways to declare function that receives array as argument.
First way:
return_type function(type arrayname[ ])
Declaring blank subscript notation [ ] is the widely used technique.
Second way:
return_type function(type arrayname[SIZE])
Optionally, we can define size in subscript notation [ ].
Third way:
return_type function(type *arrayname)
You can also use the concept of pointer.

passing array to function example

#include <stdio.h>  
int minarray(int arr[],int size)    {
int min=arr[0];
int i=0;
for(i=1;iarr[i]){
min=arr[i];
    }
   }    //end of for
return min;
   }     //end of function
  void main() {
int i=0,min=0;
int numbers[]={6,7,8,9,10};//declaration of array
min=minarray(numbers,5);//passing array with size
printf("minimum number is %d",min);
  }
   
Output:  minimum number is 6