Initialization of structures and unions

An initializer for a structure is a brace-enclosed comma-separated list of values, and for a union, a brace-enclosed single value. The initializer is preceded by an equal sign (=).

C++ allows the initializer for an automatic member variable of a union or structure type to be a constant or non-constant expression.

C++ The initializer for a static member variable of a union or structure type must be a constant expression or string literal. See Static data members (C++ only) for more information.

You can specify initializers for structures and unions:

Using C89-style initialization, the following example shows how you would initialize the first union member birthday of the union variable people:

union {
      char birthday[9];
      int age;
      float weight;
      } people = {"23/07/57"};

The following definition shows a completely initialized structure:

struct address {
                 int street_no;
                 char *street_name;
                 char *city;
                 char *prov;
                 char *postal_code;
               };
static struct address perm_address =
               { 3, "Savona Dr.", "Dundas", "Ontario", "L4B 2A1"};

The values of perm_address are:

Member Value
perm_address.street_no 3
perm_address.street_name address of string "Savona Dr."
perm_address.city address of string "Dundas"
perm_address.prov address of string "Ontario"
perm_address.postal_code address of string "L4B 2A1"

Unnamed structure or union members do not participate in initialization and have indeterminate value after initialization. Therefore, in the following example, the bit field is not initialized, and the initializer 3 is applied to member b:

struct {
     int a;
     int :10;
     int b;
     } w = { 2, 3 };

You do not have to initialize all members of a structure or union; the initial value of uninitialized structure members depends on the storage class associated with the structure or union variable. In a structure declared as static, any members that are not initialized are implicitly initialized to zero of the appropriate type; the members of a structure with automatic storage have no default initialization. The default initializer for a union with static storage is the default for the first component; a union with automatic storage has no default initialization.

The following definition shows a partially initialized structure:

struct address {
                 int street_no;
                 char *street_name;
                 char *city;
                 char *prov;
                 char *postal_code;
               };
struct address temp_address =
               { 44, "Knyvet Ave.", "Hamilton", "Ontario" };

The values of temp_address are:

Member Value
temp_address.street_no 44
temp_address.street_name address of string "Knyvet Ave."
temp_address.city address of string "Hamilton"
temp_address.prov address of string "Ontario"
temp_address.postal_code Depends on the storage class of the temp_address variable; if it is static, the value would be NULL.

Related information



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