# Confusion with 2D arrays

by Tezi   Last Updated September 11, 2019 13:24 PM

Was trying a piece of code like this below and found that things were not happening the way it was expected to be to store values in the array indices. Please check the code snippet below and the output

``````#include <stdio.h>

int main(void) {
int a;
int i,j,count_ld_x=0,count_rd_x=0;
for(i=0;i<3;i++){

for(j=0;j<3;j++){
printf("Inputing value for the element:a[%d][%d]",i,j);
scanf("%d",&a[i][j]);
}
}

for(i=0;i<3;i++){
for(j=0;j<3;j++){
printf("a[%d][%d]=%d\n",i,j,a[i][j]);
}
}
}
``````

Input and Output:-

 ./main

``````Inputing value for the element:a0
Inputing value for the element:a1
Inputing value for the element:a2
Inputing value for the element:a3
Inputing value for the element:a4
Inputing value for the element:a5
Inputing value for the element:a6
Inputing value for the element:a7
Inputing value for the element:a8
a=0
a=1
a=3
a=3
a=4
a=6
a=6
a=7
a=8
``````

Confusion:- Am storing a with 2 and the o/p am getting for the same position is 3. Can you please explain what's the issue?

Tags :