int test[3] = {1,2,3};
cout<<test[3]<<endl;
// this will get a error
but
int test[3] = {1,2,3};
int (*A)[3];
A = &test;
cout<<test[3]<<(*A)[3]<<endl;
// this will print -858993460 withiout any errors
So could anyone tell me why? i am really confused by that.
in the first case why it is not outofboundary error but an undefined error?
and why the second case will not get a error? i used to think they are the same…
actually i did know the array start from 0, i am confused by why the first got an error but the second won’t????
Undefined behaviour seems a very tricky concept for newbies to understand. But it’s very simple, if you break the rules of C++ (as both your examples do) then, very often, the behaviour of your program is undefined. It means exactly what it says and it’s pointless asking ‘why does it do that’ as there are no longer any rules or laws applicable to your situation.