W3cubDocs

/GCC 7

6.29 Cast to a Union Type

A cast to union type looks similar to other casts, except that the type specified is a union type. You can specify the type either with the union keyword or with a typedef name that refers to a union. A cast to a union actually creates a compound literal and yields an lvalue, not an rvalue like true casts do. See Compound Literals.

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:

union foo { int i; double d; };
int x;
double y;

both x and y can be cast to type union foo.

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:

union foo u;
/*  */
u = (union foo) x  ≡  u.i = x
u = (union foo) y  ≡  u.d = y

You can also use the union cast as a function argument:

void hack (union foo);
/*  */
hack ((union foo) x);

© Free Software Foundation
Licensed under the GNU Free Documentation License, Version 1.3.
https://gcc.gnu.org/onlinedocs/gcc-7.3.0/gcc/Cast-to-Union.html