An initializer for a structure is a brace-enclosed comma-separated list of values. An initializer is preceded by an equal sign (=). In the absence of designations, memory for structure members is allocated in the order declared, and memory address are assigned in increasing order, with the first component starting at the beginning address of the structure name itself. You do not have to initialize all members of a structure. The default initializer for a structure with static storage is the recursive default for each component; a structure with automatic storage has none.
The following subsection pertains to C only.
Named members of a structure can be initialized in any order; any named member of a union can be initialized, even if it is not the first member. A designator identifies the structure or union member to be initialized. The designator for a structure or union member consists of a dot and its identifier (.fieldname). A designator list is a combination of one or more designators for any of the aggregate types. A designation is a designator list followed by an equal sign (=).
A designator identifies a first subobject of the current object, which at the beginning of the initialization is the structure itself. After initializing the first subobject, the next subobject becomes the current object, and its first subobject is initialized; that is, initialization proceeds in forward order, and any previous subobject initializations are overridden.
The initializer for an automatic variable of a structure or any aggregate type can be a constant or non-constant expression. Allowing an initializer to be a constant or non-constant expression is a C99 language feature.
The following declaration of a structure is a definition that contains designators, which remove some of the ambiguity about which subobject will be initialized by providing an explicit initialization. The following declaration defines an array with two element structures. In the excerpt below, [0].a and [1].a[0] are designator lists.
struct { int a[5], b; } game[] = { [0].a = { 1 }, [1].a[0] = 2 }; /* game[0].a[0] is 1, game[1].a[0] is 2, and all other elements are zero. */
The declaration syntax uses braces to indicate initializer lists, yet is referred to as a bracketed form. A fully bracketed form of a declaration is less likely to be misunderstood than a terser form. The following definition accomplishes the same thing, is legal and shorter, but inconsistently bracketed, and could be misleading. Neither b structure member of the two struct game objects is initialized to 2.
struct { int a[5], b; } game[] = { { 1 }, 2 }; /* game[0].a[0] is 1, game[1].a[0] is 2, and all other elements are zero. */
Unnamed structure or union members do not participate in initialization and have indeterminate value after initialization.
Example
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:
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:
Related References