Structure and union type and variable definitions in a single statement

You can define a structure (or union) type and a structure (or union) variable in one statement, by putting a declarator and an optional initializer after the variable definition. The following example defines a union data type (not named) and a union variable (named length):

union {
        float meters;
        double centimeters;
        long inches;
      } length;

Note that because this example does not name the data type, length is the only variable that can have this data type. Putting an identifier after struct or union keyword provides a name for the data type and lets you declare additional variables of this data type later in the program.

To specify a storage class specifier for the variable or variables, you must put the storage class specifier at the beginning of the statement. For example:

static struct {
                 int street_no;
                 char *street_name;
                 char *city;
                 char *prov;
                 char *postal_code;
              } perm_address, temp_address;

In this case, both perm_address and temp_address are assigned static storage.

Type qualifiers can be applied to the variable or variables declared in a type definition. Both of the following examples are valid:

volatile struct class1 {
                        char descript[20];
                        long code;
                        short complete;
                     }  file1, file2;
struct class1 {
                        char descript[20];
                        long code;
                        short complete;
                     }  volatile file1, file2;

In both cases, the structures file1 and file2 are qualified as volatile.

Related information



[ Top of Page | Previous Page | Next Page | Contents | Index ]