I run the following code.
#include <stdio.h>
int main(){
int g[2][2]={{1,2},{3,4}};
//trying to print the array(y,x)
int x,y;
x=1;
y=1;
printf("element (%d,%d) of array is %d", y,x,g[y][x]);
return 0;
}
Most of it runs fine. I get the results g[0][0]=1, g[0][1]=2, g[1][0]=3, g[1][1]=4. But when I try to access elements like g[2][0], i get 2. similarly g[2][1]=1, g[0][2]=3, g[1][2]=1. I would have expected if any of the index was 2, i would get a grabage value. This does happen for (2,2) and for indices 3 and above. But I dont understand how elemnts like g[2][0] are accessed.
Can someone help me understand this? I want to understand this because I want to write a code for periodic boundary conditions where I will need to access the elements outside the array.(I am relatively new to learning C and dont thoroughly understand the pointers and 2d arrays thing.)