Which is something that hardly makes any sense and most professional will never need in their entire career.
Which is something that hardly makes any sense and most professional will never need in their entire career.
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);There is no sane reason to want to access a float as an integer.
edit: are you also expressing disagreement with the claim that few C++ programs are correct (and the implied claim that this is because many people's first-guess approaches are actually incorrect), or just saying that you think this is a bad example of the language being complex or misleading?
Actually, I wonder if you do ;-)
You insisted you want char *, by which I understood you meant bytes. Rust can do that too, but now you need to care about byte order. Seeing another answer in this sub-thread I realise you actually meant you wanted a string which is doubly hilarious in a C++ thread. The char * is a pointer to a byte, C++ has actual strings (and, these days, even a passable fat pointer style string reference called std::string_view) and they're different.