Yes, being able to fwrite() dump floats to binaries is invaluable when working with large datasets. It's common to see binary 'cache' file being created for ASCII inputs (e.g. CSV) when there is any chance of it being fed repeatedly.
That requires a cast from float* to void*. Reading the floating point number as an integer is not necessary.
Casting any object pointer to (and from) void* or char* is guaranteed to work, while float to integer incurs in the undefined behaviour curse.
for (int i=0; i<4; i++)
buf[i] = ((char*)&my_float)[i];
is not undefined behavior? But that for (int i=0; i<4; i++)
buf[i] = ((char*)(int32_t*)&my_float)[i];
is undefined?On the other side, this would incur UB:
int32_t my_int = *((int32_t*)&my_float);