|
|
|
|
|
by ithkuil
2956 days ago
|
|
Yep the correct way is: union { int i; float f;} u; u.i=*i; Now u.f contains the same bit pattern as the integer pointed to by "i" but interpreted as a float. Compilers know how to generate code efficiently out of this. Iirc this is implementation defined behavior, e.g. things like endianness alignment or padding, but it's not undefined behavior, whose evil effects can bubble up in unexpected parts of the program that apparently don't even touch the bad cast. |
|