Open main menu
Home
Random
Recent changes
Special pages
Community portal
Preferences
About Wikipedia
Disclaimers
Incubator escapee wiki
Search
User menu
Talk
Dark mode
Contributions
Create account
Log in
Editing
C syntax
(section)
Warning:
You are not logged in. Your IP address will be publicly visible if you make any edits. If you
log in
or
create an account
, your edits will be attributed to your username, along with other benefits.
Anti-spam check. Do
not
fill this in!
====Designated initializers==== Designated initializers allow members to be initialized by name, in any order, and without explicitly providing the preceding values. The following initialization is equivalent to the previous one: <syntaxhighlight lang=C>struct s pi = { .z = "Pi", .x = 3, .y = 3.1415 };</syntaxhighlight> Using a designator in an initializer moves the initialization "cursor". In the example below, if <code>MAX</code> is greater than 10, there will be some zero-valued elements in the middle of <code>a</code>; if it is less than 10, some of the values provided by the first five initializers will be overridden by the second five (if <code>MAX</code> is less than 5, there will be a compilation error): <syntaxhighlight lang=C>int a[MAX] = { 1, 3, 5, 7, 9, [MAX-5] = 8, 6, 4, 2, 0 };</syntaxhighlight> In [[C89 (C version)|C89]], a union was initialized with a single value applied to its first member. That is, the union ''u'' defined above could only have its ''int x'' member initialized: <syntaxhighlight lang=C>union u value = { 3 };</syntaxhighlight> Using a designated initializer, the member to be initialized does not have to be the first member: <syntaxhighlight lang=C>union u value = { .y = 3.1415 }; </syntaxhighlight> If an array has unknown size (i.e. the array was an [[#Incomplete_types|incomplete type]]), the number of initializers determines the size of the array and its type becomes complete: <syntaxhighlight lang=C> int x[] = { 0, 1, 2 } ;</syntaxhighlight> Compound designators can be used to provide explicit initialization when unadorned initializer lists might be misunderstood. In the example below, <code>w</code> is declared as an array of structures, each structure consisting of a member <code>a</code> (an array of 3 <code>int</code>) and a member <code>b</code> (an <code>int</code>). The initializer sets the size of <code>w</code> to 2 and sets the values of the first element of each <code>a</code>: <syntaxhighlight lang=C>struct { int a[3], b; } w[] = { [0].a = {1}, [1].a[0] = 2 };</syntaxhighlight><!-- Note: The [http://www.open-std.org/jtc1/sc22/wg14/www/docs/n1124.pdf C99 specification]'s grammar for designations reads in part: designation: designator-list = designator-list: designator designator-list designator designator: [ constant-expression ] . identifier ...which can be understood as making these code fragments legal: int x[] = { [1] [9] = 2 } ; struct t { char *name; char *nickname; } who = { .name .nickname = "Unknown" }; But that is incorrect. The designators that make up the designator-list are *not* space-separated: the grammar means to describe the kind of structure-path given in the example. --> This is equivalent to:<syntaxhighlight lang=C>struct { int a[3], b; } w[] = { { { 1, 0, 0 }, 0 }, { { 2, 0, 0 }, 0 } };</syntaxhighlight> There is no way to specify repetition of an initializer in standard C.
Edit summary
(Briefly describe your changes)
By publishing changes, you agree to the
Terms of Use
, and you irrevocably agree to release your contribution under the
CC BY-SA 4.0 License
and the
GFDL
. You agree that a hyperlink or URL is sufficient attribution under the Creative Commons license.
Cancel
Editing help
(opens in new window)