Suppose we have the following program:
#include <iostream>
int main()
{
int a[2][3]{{1, 2, 3},
{4, 5, 6}};
a[0][4] = -50;
std::cout << a[1][1] << std::endl; // prints -50
}
Is this guaranteed to work or it is undefined behavior?
As long as I know it is guaranteed that &a[0][2] + 1 == &a[1][0]
, but on the other side a[0]
is an array itself and the valid indices for it are 0..2
.
Edit: If it is forbidden by the standard, is there any specific reason to be forbidden? The 'duplicate' question does not answer that. I need to know the technical reason behind this. Why would it be worse if it was allowed?