Programming Logics FORUMS

Full Version: 2d Array Confused
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
int array[3][3]={{1,2,3},{4,5,6},{7,8,9}};

cout<<array[0][6];

output is 7 but how is it possible ... ?? is it out of bound ??
Its very simple
C++ will calculate the 2D Array using following Formula in case of array[0][6]

array + 0*3 + 6

0 is required row and 6 is required column , 3 is total rows
As C++ store data in row major then in this case it will skip 6 memory locations and result will be 7
It is absolutely out of bound but in the memory of the 7th place form the variable array must have some value. Weather it is garbage value but something is stored there, thats why it gives you the answer. It is a garbage value.
Reference URL's