Forced conversion of pointer type, forced pointer type
The problem is:
# Include <iostream. h>
Void main (){
Int k = 1;
Int * f = & k;
* (Float *) (f) = 1;
Cout <* f;
} // Why is the output not 1?
At this time, we can see that there is format conversion in the function Format. If there are two or more formats in addition to the void format during format conversion, in this case, the memory occupied by the variable indicated by the pointer changes accordingly.
For this question:
When you forcibly change the int pointer to the float pointer, the data actually goes beyond the memory space range defined by the int.
For example, int occupies 4 bytes, float occupies 8 bytes, and k = 1 is stored in 4 bytes.
When you convert the pointer to float, it will read the four bytes of 1 and the additional four bytes following it, so it is definitely not 1. What should be random?