Confusion with 2D arrays

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

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[2][2];
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:a[0][0]0
Inputing value for the element:a[0][1]1
Inputing value for the element:a[0][2]2
Inputing value for the element:a[1][0]3
Inputing value for the element:a[1][1]4
Inputing value for the element:a[1][2]5
Inputing value for the element:a[2][0]6
Inputing value for the element:a[2][1]7
Inputing value for the element:a[2][2]8
a[0][0]=0
a[0][1]=1
a[0][2]=3
a[1][0]=3
a[1][1]=4
a[1][2]=6
a[2][0]=6
a[2][1]=7
a[2][2]=8 

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



Related Questions


HTML Hreflang Errors in Search Console - No Return Tags

Updated February 28, 2019 17:24 PM


Add option to search posts with 'Stack Snippets'

Updated February 19, 2016 01:10 AM