First thing to note is that you're accessing a signed integer variable through a pointer to an unsigned integer variable. Also note that sizeof(size_t)
may not necessarily equal sizeof(int)
!
The line:
printf("%zu\n", ((size_t*)ptr) - *((size_t*)ptr));
Effectively Does AddressOf(i[2]) - i[2]
, which is AddressOf(i[2]) - 1
.
In C pointer arithmetic the address is decremented not by 1, but by 1 * sizeof(data type)
, which is whatever sizeof(size_t)
is on your machine. If you're on a 64-bit machine this could be 8 bytes. This the address you get will be -8
and not, as I think you're expecting, -1
...
EDIT: The %z
could probably be %p
for pointer addresses. The casts to size_t *
are not needed and could be dangerous as discussed above...