[Main]
Title=Cast to a Union Type

[Top]
A cast to union type is similar to other casts, except that the type
specified is a union type.  You can specify the type either with
<CODE><A HREF="$$INFOLINK(keywords/union)">union</A>&nbsp;<I>tag</I></CODE> or with a typedef name.  A cast to union is actually
a constructor though, not a cast, and hence does not yield an lvalue like
normal casts (see <A HREF="$$LINK(SEC81)">Compound Literals</A>).
<BR><BR>
The types that may be cast to the union type are those of the members
of the union.  Thus, given the following union and variables:
<PRE>union foo { int i; double d; };
int x;
double y;
</PRE>
both <I>x</I> and <I>y</I> can be cast to type <CODE><A HREF="$$INFOLINK(keywords/union)">union</A>&nbsp;foo</CODE>.
<BR><BR>
Using the cast as the right-hand side of an assignment to a variable of
union type is equivalent to storing in a member of the union:
<PRE>union foo u;
/* ... */
u = (union foo) x  &lt;=&gt;  u.i = x
u = (union foo) y  &lt;=&gt;  u.d = y
</PRE>
You can also use the union cast as a function argument:
<PRE>void hack (union foo);
/* ... */
hack ((union foo) x);
</PRE>
