Does C23 officially allow type-punning through unions?
hellooo, here to ask about a low-level networking thing and i need to reinterpret a uint32_t as float without getting undefined behavior. the union type i use:
union {
uint32\_t u;
float f;
} pun = { .u = 0x3f800000 };
printf("%f\\n", pun.f);
this gives me undefined behavior in c99, c11, c17 because you read a different union member from you wrote. compilers like gcc and clang defined it anyway as an extension.
memcpy() is there, i know but this adds overhead (even though compilers optimizes it out usually). and yeah in kernel code or freestanding environments memcpy() is not always available.
what im looking for is:
has c23 any type-punning through unions for my case? when i googled it, i see discussions about n2530 and n2654 but there is no way to be sure if it made it into the final standart. maybe there is a better way to do this in portable c without relying on compiler extensions or memcpy. idk. even though seems to work on gcc 14, clang18, i want standart compliant, not just works on my machine.