The problem is that when you do pointer arithmetic, it increases by a few of the size of the data type.
So what you do is add a square sizeof(struct test)
.
Since sizeof(struct test) = 6
, you increase the address by 6 * 6 = 36
. Therefore, you get 0x1024
and 0xfdc
instead of 0x1006
and 0xffa
. (You also switched +
and -
, but this is a trifle.)
Instead, simply do the following:
printf("%x %p %p\n", sizeof(struct test), a + 1, a - 1);
Mysticial
source share