I have the following code:
int ia[3][4] = { //
{0, 1, 2, 3}, //
{4, 5, 6, 7}, //
{8, 9, 10, 11} //
};
int (*p4)[4] = ia;
cout << \"(*(p4 + 0))
I don't understand the last one how it arrives at 1.
Undefined behavior got you there.
Due to operator precedence (the array indexing operator binds tighter than the pointer dereference operator),
*(p4 + 0)[3]
is the same as:
*((p4 + 0)[3])
, which is the same as:
*(p4[3])
, which is the same as:
p4[3][0]
.
For your array. the valid indices for the first dimension are: 0
, 1
, and 2
. Accessing the array using the index value of 3
accesses memory beyond valid range, leading to undefined behavior.