Would a union do the trick?
union { int i; float f } u { .f = 1.23f };
int i = u.i;
Another way is a memcpy, which I believe is the most defined way to do type punning int i;
float f;
memcpy(&i, &f, 4);
But you also have to assume the size of those primitive types but that's pretty safe in modern C/C++.For more information, see the C++20 final draft[0§7.6.1.9]
It’s C++ that’s lacking this feature. Not C.
https://gcc.gnu.org/onlinedocs/gcc/Optimize-Options.html#Typ...
> you also have to assume the size of those primitive types
This usually works though there's some situations where you could still be surprised. For instance if you're programming embedded processors with avr-gcc you'll have sizeof(double)==4