Path: utzoo!utgpu!water!watmath!atbowler
From: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG])
Newsgroups: comp.lang.c
Subject: Re: Union type conversions
Message-ID: <19845@watmath.waterloo.edu>
Date: 13 Jul 88 15:41:22 GMT
References: <5754@bloom-beacon.MIT.EDU> <1180@mcgill-vision.UUCP>
Reply-To: atbowler@watmath.waterloo.edu (Alan T. Bowler [SDG])
Organization: U. of Waterloo, Ontario
Lines: 22

In article <1180@mcgill-vision.UUCP> mouse@mcgill-vision.UUCP (der Mouse) writes:
>In article <5754@bloom-beacon.MIT.EDU>, tada@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.