Union type conversions

Alan T. Bowler [SDG] atbowler at watmath.waterloo.edu
Thu Jul 14 01:41:22 AEST 1988


In article <1180 at mcgill-vision.UUCP> mouse at mcgill-vision.UUCP (der Mouse) writes:
>In article <5754 at bloom-beacon.MIT.EDU>, tada at athena.mit.edu (Michael Zehr) writes:
>> So what it boils down to, is whether casting into a union type is
>> legal and portable
>
>Ouch.  I just searched through K&R V2 for a description of what may be
>cast to what.  Nowhere did I find anything that comes right out and
>*says* you can't cast to an aggregate type.  However, I also found
>nothing explicitly requiring it to even compile, much less work.

Actually I dn't think you are guaranteed anything more than
if you assign to a particular union member you can get back the
value you assigned by naming that member provided that you do
no assign to any other member.  It is usual practice for a compiler
to put all members of a union at the same starting address
(i.e. equivalence them) however, there is no guarantee that
the compiler does not simply do the equivalent of
#define union struct
and proceed from there.  Using union for a "pun" operation
is  reasonable thing to do in many programs, however you
should always be aware that it is not a machine independant
action and would need to be checked in porting a program.



More information about the Comp.lang.c mailing list