double d = 2.5; auto p = (int*)&d; auto q = &d;
p and q are created by pointing to the same memory location. The memory contains double (usually 8 bytes)
While creating
auto p = (int*)&d;
you tell the compiler ( reintepret_cast< int*> ( &d) ) that the value in d was an integer.
Pointer values ββare the same, but types are not.
When you print
cout<<*q<<endl;
You show the correct value - as it appears and exits.
when printing
cout<<*p<<endl;
You look at 4 (usually) bytes of 8-byte memory and interpret them as an integer.
It will be 0x00, 0x00, 0x00, 0x00
mksteve
source share