As others have pointed out, this behavior is undefined. However, there is a very simple explanation for what you see.
The difference between pointers is the number of elements, not the number of bytes between them.
pi and pi1 point to longs, but the address that pi1 points to is only one byte, except for pi. Assuming longs is 4 bytes long, the difference in addresses 1 divided by the size of element 4 is 0.
Another way to think about what you could imagine is that the compiler would generate code equivalent to this for computing d1:
int d1 = ((BYTE*)pi1 - (BYTE*)pi)/sizeof(long).
Michael
source share