summaryrefslogtreecommitdiff
path: root/devdocs/gcc~13/designated-inits.html
diff options
context:
space:
mode:
Diffstat (limited to 'devdocs/gcc~13/designated-inits.html')
-rw-r--r--devdocs/gcc~13/designated-inits.html22
1 files changed, 22 insertions, 0 deletions
diff --git a/devdocs/gcc~13/designated-inits.html b/devdocs/gcc~13/designated-inits.html
new file mode 100644
index 00000000..dcb5f8ff
--- /dev/null
+++ b/devdocs/gcc~13/designated-inits.html
@@ -0,0 +1,22 @@
+<div class="section-level-extent" id="Designated-Inits"> <div class="nav-panel"> <p> Next: <a href="case-ranges" accesskey="n" rel="next">Case Ranges</a>, Previous: <a href="compound-literals" accesskey="p" rel="prev">Compound Literals</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Designated-Initializers"><span>6.29 Designated Initializers<a class="copiable-link" href="#Designated-Initializers"> ¶</a></span></h1> <p>Standard C90 requires the elements of an initializer to appear in a fixed order, the same as the order of the elements in the array or structure being initialized. </p> <p>In ISO C99 you can give the elements in any order, specifying the array indices or structure field names they apply to, and GNU C allows this as an extension in C90 mode as well. This extension is not implemented in GNU C++. </p> <p>To specify an array index, write ‘<samp class="samp">[<var class="var">index</var>] =</samp>’ before the element value. For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int a[6] = { [4] = 29, [2] = 15 };</pre>
+</div> <p>is equivalent to </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int a[6] = { 0, 0, 15, 0, 29, 0 };</pre>
+</div> <p>The index values must be constant expressions, even if the array being initialized is automatic. </p> <p>An alternative syntax for this that has been obsolete since GCC 2.5 but GCC still accepts is to write ‘<samp class="samp">[<var class="var">index</var>]</samp>’ before the element value, with no ‘<samp class="samp">=</samp>’. </p> <p>To initialize a range of elements to the same value, write ‘<samp class="samp">[<var class="var">first</var> ... <var class="var">last</var>] = <var class="var">value</var></samp>’. This is a GNU extension. For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };</pre>
+</div> <p>If the value in it has side effects, the side effects happen only once, not for each initialized field by the range initializer. </p> <p>Note that the length of the array is the highest value specified plus one. </p> <p>In a structure initializer, specify the name of a field to initialize with ‘<samp class="samp">.<var class="var">fieldname</var> =</samp>’ before the element value. For example, given the following structure, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct point { int x, y; };</pre>
+</div> <p>the following initialization </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct point p = { .y = yvalue, .x = xvalue };</pre>
+</div> <p>is equivalent to </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct point p = { xvalue, yvalue };</pre>
+</div> <p>Another syntax that has the same meaning, obsolete since GCC 2.5, is ‘<samp class="samp"><var class="var">fieldname</var>:</samp>’, as shown here: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct point p = { y: yvalue, x: xvalue };</pre>
+</div> <p>Omitted fields are implicitly initialized the same as for objects that have static storage duration. </p> <p>The ‘<samp class="samp">[<var class="var">index</var>]</samp>’ or ‘<samp class="samp">.<var class="var">fieldname</var></samp>’ is known as a <em class="dfn">designator</em>. You can also use a designator (or the obsolete colon syntax) when initializing a union, to specify which element of the union should be used. For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">union foo { int i; double d; };
+
+union foo f = { .d = 4 };</pre>
+</div> <p>converts 4 to a <code class="code">double</code> to store it in the union using the second element. By contrast, casting 4 to type <code class="code">union foo</code> stores it into the union as the integer <code class="code">i</code>, since it is an integer. See <a class="xref" href="cast-to-union">Cast to a Union Type</a>. </p> <p>You can combine this technique of naming elements with ordinary C initialization of successive elements. Each initializer element that does not have a designator applies to the next consecutive element of the array or structure. For example, </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int a[6] = { [1] = v1, v2, [4] = v4 };</pre>
+</div> <p>is equivalent to </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int a[6] = { 0, v1, v2, 0, v4, 0 };</pre>
+</div> <p>Labeling the elements of an array initializer is especially useful when the indices are characters or belong to an <code class="code">enum</code> type. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">int whitespace[256]
+ = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
+ ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };</pre>
+</div> <p>You can also write a series of ‘<samp class="samp">.<var class="var">fieldname</var></samp>’ and ‘<samp class="samp">[<var class="var">index</var>]</samp>’ designators before an ‘<samp class="samp">=</samp>’ to specify a nested subobject to initialize; the list is taken relative to the subobject corresponding to the closest surrounding brace pair. For example, with the ‘<samp class="samp">struct point</samp>’ declaration above: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };</pre>
+</div> <p>If the same field is initialized multiple times, or overlapping fields of a union are initialized, the value from the last initialization is used. When a field of a union is itself a structure, the entire structure from the last field initialized is used. If any previous initializer has side effect, it is unspecified whether the side effect happens or not. Currently, GCC discards the side-effecting initializer expressions and issues a warning. </p> </div> <div class="nav-panel"> <p> Next: <a href="case-ranges">Case Ranges</a>, Previous: <a href="compound-literals">Compound Literals</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution">
+ <p class="_attribution-p">
+ &copy; Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br>
+ <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Designated-Inits.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Designated-Inits.html</a>
+ </p>
+</div>