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 (=
).
C99 and C++ allow the initializer for an automatic member variable of a union or structure type to be a constant or non-constant expression.
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.
- With C89-style initializers, structure members must be initialized in the order declared, and only the first member of a union can be initialized.
- Using designated initializers, a C99 feature which allows you to name members to be initialized, structure members can be initialized in any order, and any (single) member of a union can be initialized. Designated initializers are described in detail in Designated initializers for aggregate types (C only).
birthday
of
the union variable people
: union {
char birthday[9];
int age;
float weight;
} people = { "23⁄07⁄57" };
age
: union {
char birthday[9];
int age;
float weight;
} people = { .age = 14 };
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" |
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.
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 . |
temp_address
variable,
you could use a designated initializer list, as follows: struct address {
int street_no;
char *street_name;
char *city;
char *prov;
char *postal_code;
};
struct address temp_address =
{ .city = "Hamilton", .prov = "Ontario" };