In your statement:
char ch=*(char*)&d;
You are performing the following:
1. Taking the address or location of the variable.
2. Creating a pointer of type character to point to the variable.
3. Dereferencing (converting the first location of the variable) to a type of character (no transformations have occurred yet.
4. Assigning the first memory location of d
to a character type variable (no conversions yet).
You are then printing the value in ch
.
If the value in ch
is a printable value, you are in luck; however, I doubt it will be meaningful. Otherwise, you won't be able to see it.
If you want to convert or transform from internal representation to textual representation, use:
cout << d;
I highly recommend using a debugger on this code and view the memory location of variable d
. Take the value and see if it is printable.
Edit 1:
The value printed in line 4 depends on how the value 3.1416
is stored in memory.
Since it is a floating point variable, we can guess that it is split into: sign, mantissa and exponent. The big question, is how many bits are dedicated to the three groups. Usually, one bit is the sign. The others depend on your platform. If the char
type on your platform is 8 bit, and the order is mantissa, exponent, then sign, your character will be the first 8 bits of the mantissa, which is probably not a printable character.