why the value of dz changes in case [2] while it remains same in case [1] ? |
Blind luck.
You define dz as a double. That means the bits within dz have specific meanings that, taken together, represent floating point numbers.
1 2
|
int* p = (int*)(&dz);
*p = 432344332;
| |
says "pretend that dz contains an int, not a double. Now store 432344332 in the bytes at dz." In other words, it stores the bit pattern representing integer 432344332 into the location where dz is stored.
Since integers and doubles use very different bit patterns (and even different numbers of bits!), you've basically now stored garbage in dz.
The fact that the first example results in no change in value is a red herring. Someone figured out exactly what bit pattern encodes the double 23.4, figured out what the corresponding bits represent when interpreted as an integer, and used that in the code. After all, isn't 432344332 a strange number to store?
So why does the second example produce a different result? Because ints and longs use different bit encodings (specifically, they probably use different numbers of bytes).