C Two-Dimensional Arrays
C Two-Dimensional Arrays
A two-dimensional array is an array of dimension 2 (we can also call a 2D array), which is a collection of elements accessed using two subscript expressions. For example, let’s call them rows and cols. A two-dimensional array is also called an array of single-dimensional arrays.
Declare 2D Array
The general syntax to declare 2D Array is:
<data type> <array name>[rows][cols];
int arr[2][2];
The array index in both dimensions starts with 0.
Example
#include<stdio.h>
void main(void){
// 2D Array declaration
int arr[2][2];
int i,j;
printf(“Enter Array elements:= “);
for(i = 0;i<2;i++){
for(j=0;j<2 ;j++) {
scanf(ā%dā,&arr[i][j]); }
}
for(i = 0;i<2;i++){
for(j=0;j<2;j++) {
printf(“Array element = %d\n”,arr[i][j]);
}
}
}