Quantcast
Viewing latest article 27
Browse Latest Browse All 39

Answer by Alex Lop. for C - calculate address of pointer

I can explain how to calculate the address with few assumptions:

  1. The size of int is 4 bytes
  2. The system is little-endian
  3. 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

Viewing latest article 27
Browse Latest Browse All 39

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>