| United States-English |
|
|
|
![]() |
HP C/HP-UX Reference Manual: HP-UX Systems > Chapter 3 Data
Types and Declarations Designated Initializer |
|
Designated initializer specifically initialize a particular member in a struct, union, or a particular element of an array. Designated initializer is a part of the C99 standards (ISO/IEC 9899:1999:6.7.8). Designated initializer eliminate the need to initialize the members of aggregates in order (from the first member onto the last in a structure and from the element indexed 0 to the last index in an array). Designated initializer is of the form:
The following examples detail the usage of designated initializers in the C program: int a[100] = {1,2,[50]=3,4,5,[23]=6,7}; In this example, designated initializers [50] and [23] are used to initialize elements in the middle of an array without initializing all the elements prior to them. The element a[50] contains the value 3 while a[51] is initialized to 4. Similarly, a[23]=6 and a[24]=7. struct node In this example, .b and .a initialize particular members of the structure st while the designated initializer.c[4] initializes a particular array element (index 4) of the structure member.c[4] with 1, c[5] with 2, and so on. union u In the above example, the order of initializing union members need not be followed using designated initializers. struct st { The above example shows how member-wise initialization can be done using a structure and how initialization was done earlier. struct st{int a[5], b;}; In this example, a combination of both array and structure are initialized. Here w[1].a[3] has a value of 1. |
|||||||||||||||||||||||||||||||||||||||||||
|
|||||||||||||||