1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Designated Inits (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Designated Inits (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Designated Inits (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Variables.html" rel="up" title="Variables">
|
---|
33 | <link href="Auto-Type.html" rel="next" title="Auto Type">
|
---|
34 | <link href="Initializers.html" rel="prev" title="Initializers">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Designated-Inits"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Auto-Type.html" accesskey="n" rel="next">Auto Type</a>, Previous: <a href="Initializers.html" accesskey="p" rel="prev">Initializers</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Designated-Initializers"></span><h3 class="section">20.3 Designated Initializers</h3>
|
---|
64 | <span id="index-initializers-with-labeled-elements"></span>
|
---|
65 | <span id="index-labeled-elements-in-initializers"></span>
|
---|
66 | <span id="index-case-labels-in-initializers"></span>
|
---|
67 | <span id="index-designated-initializers"></span>
|
---|
68 |
|
---|
69 | <p>In a complex structure or long array, it’s useful to indicate
|
---|
70 | which field or element we are initializing.
|
---|
71 | </p>
|
---|
72 | <p>To designate specific array elements during initialization, include
|
---|
73 | the array index in brackets, and an assignment operator, for each
|
---|
74 | element:
|
---|
75 | </p>
|
---|
76 | <div class="example">
|
---|
77 | <pre class="example">int foo[10] = { [3] = 42, [7] = 58 };
|
---|
78 | </pre></div>
|
---|
79 |
|
---|
80 | <p>This does the same thing as:
|
---|
81 | </p>
|
---|
82 | <div class="example">
|
---|
83 | <pre class="example">int foo[10] = { 0, 0, 0, 42, 0, 0, 0, 58, 0, 0 };
|
---|
84 | </pre></div>
|
---|
85 |
|
---|
86 | <p>The array initialization can include non-designated element values
|
---|
87 | alongside designated indices; these follow the expected ordering
|
---|
88 | of the array initialization, so that
|
---|
89 | </p>
|
---|
90 | <div class="example">
|
---|
91 | <pre class="example">int foo[10] = { [3] = 42, 43, 44, [7] = 58 };
|
---|
92 | </pre></div>
|
---|
93 |
|
---|
94 | <p>does the same thing as:
|
---|
95 | </p>
|
---|
96 | <div class="example">
|
---|
97 | <pre class="example">int foo[10] = { 0, 0, 0, 42, 43, 44, 0, 58, 0, 0 };
|
---|
98 | </pre></div>
|
---|
99 |
|
---|
100 | <p>Note that you can only use constant expressions as array index values,
|
---|
101 | not variables.
|
---|
102 | </p>
|
---|
103 | <p>If you need to initialize a subsequence of sequential array elements to
|
---|
104 | the same value, you can specify a range:
|
---|
105 | </p>
|
---|
106 | <div class="example">
|
---|
107 | <pre class="example">int foo[100] = { [0 ... 19] = 42, [20 ... 99] = 43 };
|
---|
108 | </pre></div>
|
---|
109 |
|
---|
110 | <p>Using a range this way is a GNU C extension.
|
---|
111 | </p>
|
---|
112 | <p>When subsequence ranges overlap, each element is initialized by the
|
---|
113 | last specification that applies to it. Thus, this initialization is
|
---|
114 | equivalent to the previous one.
|
---|
115 | </p>
|
---|
116 | <div class="example">
|
---|
117 | <pre class="example">int foo[100] = { [0 ... 99] = 43, [0 ... 19] = 42 };
|
---|
118 | </pre></div>
|
---|
119 |
|
---|
120 | <p>as the second overrides the first for elements 0 through 19.
|
---|
121 | </p>
|
---|
122 | <p>The value used to initialize a range of elements is evaluated only
|
---|
123 | once, for the first element in the range. So for example, this code
|
---|
124 | </p>
|
---|
125 | <div class="example">
|
---|
126 | <pre class="example">int random_values[100]
|
---|
127 | = { [0 ... 99] = get_random_number() };
|
---|
128 | </pre></div>
|
---|
129 |
|
---|
130 | <p>would initialize all 100 elements of the array <code>random_values</code> to
|
---|
131 | the same value—probably not what is intended.
|
---|
132 | </p>
|
---|
133 | <p>Similarly, you can initialize specific fields of a structure variable
|
---|
134 | by specifying the field name prefixed with a dot:
|
---|
135 | </p>
|
---|
136 | <div class="example">
|
---|
137 | <pre class="example">struct point { int x; int y; };
|
---|
138 |
|
---|
139 | struct point foo = { .y = 42; };
|
---|
140 | </pre></div>
|
---|
141 |
|
---|
142 | <p>The same syntax works for union variables as well:
|
---|
143 | </p>
|
---|
144 | <div class="example">
|
---|
145 | <pre class="example">union int_double { int i; double d; };
|
---|
146 |
|
---|
147 | union int_double foo = { .d = 34 };
|
---|
148 | </pre></div>
|
---|
149 |
|
---|
150 | <p>This casts the integer value 34 to a double and stores it
|
---|
151 | in the union variable <code>foo</code>.
|
---|
152 | </p>
|
---|
153 | <p>You can designate both array elements and structure elements in
|
---|
154 | the same initialization; for example, here’s an array of point
|
---|
155 | structures:
|
---|
156 | </p>
|
---|
157 | <div class="example">
|
---|
158 | <pre class="example">struct point point_array[10] = { [4].y = 32, [6].y = 39 };
|
---|
159 | </pre></div>
|
---|
160 |
|
---|
161 | <p>Along with the capability to specify particular array and structure
|
---|
162 | elements to initialize comes the possibility of initializing the same
|
---|
163 | element more than once:
|
---|
164 | </p>
|
---|
165 | <div class="example">
|
---|
166 | <pre class="example">int foo[10] = { [4] = 42, [4] = 98 };
|
---|
167 | </pre></div>
|
---|
168 |
|
---|
169 | <p>In such a case, the last initialization value is retained.
|
---|
170 | </p>
|
---|
171 | <hr>
|
---|
172 | <div class="header">
|
---|
173 | <p>
|
---|
174 | Next: <a href="Auto-Type.html" accesskey="n" rel="next">Auto Type</a>, Previous: <a href="Initializers.html" accesskey="p" rel="prev">Initializers</a>, Up: <a href="Variables.html" accesskey="u" rel="up">Variables</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
175 | </div>
|
---|
176 |
|
---|
177 |
|
---|
178 |
|
---|
179 | </body>
|
---|
180 | </html>
|
---|