I can explain how to calculate the address with few assumptions:
- The size of
int
is 4 bytes - The system is little-endian
- There are no 0 paddings in the struct
Thus the locals
memory layout is so that the first 4 bytes (Z.x
) are
byte[0] = 0x42byte[1] = 0x43byte[2] = 0x44byte[3] = 0x00
and the next 4 bytes (Z.y
) are
byte[0] = 0x04byte[1] = 0x03byte[2] = 0x02byte[3] = 0x01
Now for p = ((cahr *)&Z) + 4
, p
would point to the beginning of Z
plus 4 bytes, which brings us to byte[0]
of Z.y
.
Now about
printf("%s\n", (p - *p));
*p
would be the value of the first byte of Z.y
which is 0x04
. And printf
will get as the second argument address of Z.y
- 4 bytes which is byte[0]
of Z.x
.
Thus the output of printf
will be all characters till the first '\0'
(which is the 3rd byte of Z.x
):ASCII 0x42 ASCII 0x43 ASCII 0x44:
BCD