6.31 Cast to a Union Type

A cast to a union type is a C extension not available in C++. It looks just like ordinary casts with the constraint 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. The result of a cast to a union is a temporary rvalue of the union type with a member whose type matches that of the operand initialized to the value of the operand. The effect of a cast to a union is similar to a compound literal except that it yields an rvalue like standard casts do. See Compound Literals.

Expressions that may be cast to the union type are those whose type matches at least one of the members of the union. Thus, given the following union and variables:

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

both x and y can be cast to type union foo and the following assignments

  z = (union foo) x;
  z = (union foo) y;

are shorthand equivalents of these

  z = (union foo) { .i = x };
  z = (union foo) { .d = y };

However, (union foo) FLT_MAX; is not a valid cast because the union has no member of type float.

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 with the same type

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);