How to type-pun and why

Mattias Engdegård mattias@virtutech.se
Mon Jun 12 18:04:00 GMT 2006


Andrew Haley <aph@redhat.com> writes:

>It's a gcc extension to ISO C.  

I only found "union casts" among the relevant extensions, but perhaps they
imply that the union trick work? (I'm reluctant to use union casts as they
are illegal outside gcc, but the "union trick" seems to be customary.)

It is tempting to abbreviate the union trick by using pointers, like:

  double d = something;
  uint64_t x = ((union { double d; uint64_t x; } *)&d)->x;

which _seems_ to work - but this is purely anecdotal evidence.
Would this also be guaranteed by the same gcc extension?



More information about the Gcc-help mailing list