XDR unions are discriminated unions, and do not look like C unions. They are more similar to Pascal variant records.
union-definition: "union" union-ident "switch" "("simple declaration")" "{" case-list "}" case-list: "case" value ":" declaration ";" "case" value ":" declaration ";" case-list "default" ":" declaration ";"
The following example is of a type returned as the result of a “read data” operation: if no error occurs, return a block of data. Otherwise, don't return anything.
union read_result switch (int errno) { case 0: opaque data[1024]; default: void; };
This union compiles into the following:
struct read_result { int errno; union { char data[1024]; } read_result_u; }; typedef struct read_result read_result;
Notice that the union component of the output struct
has the same name as the type name, except for the trailing _u
.