Skip to main content

XDR Data Types: Structures, Unions, and Optional Data

3.13 Structure​

Structures are declared as follows:

struct {
component-declaration-A;
component-declaration-B;
...
} identifier;

The components of the structure are encoded in the order of their declaration in the structure. Each component's size is a multiple of four bytes, though the components may be different sizes.

+-------------+-------------+...
| component A | component B |... STRUCTURE
+-------------+-------------+...

3.14 Discriminated Union​

A discriminated union is a type composed of a discriminant followed by a type selected from a set of prearranged types according to the

value of the discriminant. The type of discriminant is either "int", "unsigned int", or an enumerated type, such as "bool". The component types are called "arms" of the union, and are preceded by the value of the discriminant which implies their encoding. Discriminated unions are declared as follows:

union switch (discriminant-declaration) {
case discriminant-value-A:
arm-declaration-A;
case discriminant-value-B:
arm-declaration-B;
...
default: default-declaration;
} identifier;

Each "case" keyword is followed by a legal value of the discriminant. The default arm is optional. If it is not specified, then a valid encoding of the union cannot take on unspecified discriminant values. The size of the implied arm is always a multiple of four bytes.

The discriminated union is encoded as its discriminant followed by the encoding of the implied arm.

  0   1   2   3
+---+---+---+---+---+---+---+---+
| discriminant | implied arm | DISCRIMINATED UNION
+---+---+---+---+---+---+---+---+
|<---4 bytes--->|

3.15 Void​

An XDR void is a 0-byte quantity. Voids are useful for describing operations that take no data as input or no data as output. They are also useful in unions, where some arms may contain data and others do not. The declaration is simply as follows:

void;

Voids are illustrated as follows:

  ++
|| VOID
++
--><-- 0 bytes

3.16 Constant​

The data declaration for a constant follows this form:

const name-identifier = n;

"const" is used to define a symbolic name for a constant; it does not declare any data. The symbolic constant may be used anywhere a regular constant may be used. For example, the following defines a symbolic constant DOZEN, equal to 12.

const DOZEN = 12;

3.17 Typedef​

"typedef" does not declare any data either, but serves to define new identifiers for declaring data. The syntax is:

typedef declaration;

The new type name is actually the variable name in the declaration part of the typedef. For example, the following defines a new type called "eggbox" using an existing type called "egg":

typedef egg eggbox[DOZEN];

Variables declared using the new type name have the same type as the new type name would have in the typedef, if it was considered a variable. For example, the following two declarations are equivalent in declaring the variable "fresheggs":

eggbox  fresheggs;
egg fresheggs[DOZEN];

When a typedef involves a struct, enum, or union definition, there is another (preferred) syntax that may be used to define the same type. In general, a typedef of the following form:

typedef <<struct, union, or enum definition>> identifier;

may be converted to the alternative form by removing the "typedef" part and placing the identifier after the "struct", "union", or "enum" keyword, instead of at the end. For example, here are the two ways to define the type "bool":

typedef enum {    /* using typedef */
FALSE = 0,
TRUE = 1
} bool;

enum bool { /* preferred alternative */
FALSE = 0,
TRUE = 1
};

The reason this syntax is preferred is one does not have to wait until the end of a declaration to figure out the name of the new type.

3.18 Optional Data​

Optional-data is one kind of union that occurs so frequently that we give it a special syntax of its own for declaring it. It is declared as follows:

type-name *identifier;

This is equivalent to the following union:

union switch (bool opted) {
case TRUE:
type-name element;
case FALSE:
void;
} identifier;

It is also equivalent to the following variable-length array declaration, since the boolean "opted" can be interpreted as the length of the array:

type-name identifier<1>;

Optional-data is not so interesting in itself, but it is very useful for describing recursive data-structures such as linked-lists and trees. For example, the following defines a type "stringlist" that encodes lists of arbitrary length strings:

struct *stringlist {
string item<>;
stringlist next;
};

It could have been equivalently declared as the following union:

union stringlist switch (bool opted) {
case TRUE:
struct {
string item<>;
stringlist next;
} element;
case FALSE:
void;
};

or as a variable-length array:

struct stringlist<1> {
string item<>;
stringlist next;
};

Both of these declarations obscure the intention of the stringlist type, so the optional-data declaration is preferred over both of them. The optional-data type also has a close correlation to how recursive data structures are represented in high-level languages such as Pascal or C by use of pointers. In fact, the syntax is the same as that of the C language for pointers.