1<html lang="en"> 2<head> 3<title>Designated Inits - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Compound-Literals.html#Compound-Literals" title="Compound Literals"> 10<link rel="next" href="Case-Ranges.html#Case-Ranges" title="Case Ranges"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988-2013 Free Software Foundation, Inc. 14 15Permission is granted to copy, distribute and/or modify this document 16under the terms of the GNU Free Documentation License, Version 1.3 or 17any later version published by the Free Software Foundation; with the 18Invariant Sections being ``Funding Free Software'', the Front-Cover 19Texts being (a) (see below), and with the Back-Cover Texts being (b) 20(see below). A copy of the license is included in the section entitled 21``GNU Free Documentation License''. 22 23(a) The FSF's Front-Cover Text is: 24 25 A GNU Manual 26 27(b) The FSF's Back-Cover Text is: 28 29 You have freedom to copy and modify this GNU Manual, like GNU 30 software. Copies published by the Free Software Foundation raise 31 funds for GNU development.--> 32<meta http-equiv="Content-Style-Type" content="text/css"> 33<style type="text/css"><!-- 34 pre.display { font-family:inherit } 35 pre.format { font-family:inherit } 36 pre.smalldisplay { font-family:inherit; font-size:smaller } 37 pre.smallformat { font-family:inherit; font-size:smaller } 38 pre.smallexample { font-size:smaller } 39 pre.smalllisp { font-size:smaller } 40 span.sc { font-variant:small-caps } 41 span.roman { font-family:serif; font-weight:normal; } 42 span.sansserif { font-family:sans-serif; font-weight:normal; } 43--></style> 44<link rel="stylesheet" type="text/css" href="../cs.css"> 45</head> 46<body> 47<div class="node"> 48<a name="Designated-Inits"></a> 49<p> 50Next: <a rel="next" accesskey="n" href="Case-Ranges.html#Case-Ranges">Case Ranges</a>, 51Previous: <a rel="previous" accesskey="p" href="Compound-Literals.html#Compound-Literals">Compound Literals</a>, 52Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 53<hr> 54</div> 55 56<h3 class="section">6.26 Designated Initializers</h3> 57 58<p><a name="index-initializers-with-labeled-elements-2567"></a><a name="index-labeled-elements-in-initializers-2568"></a><a name="index-case-labels-in-initializers-2569"></a><a name="index-designated-initializers-2570"></a> 59Standard C90 requires the elements of an initializer to appear in a fixed 60order, the same as the order of the elements in the array or structure 61being initialized. 62 63 <p>In ISO C99 you can give the elements in any order, specifying the array 64indices or structure field names they apply to, and GNU C allows this as 65an extension in C90 mode as well. This extension is not 66implemented in GNU C++. 67 68 <p>To specify an array index, write 69‘<samp><span class="samp">[</span><var>index</var><span class="samp">] =</span></samp>’ before the element value. For example, 70 71<pre class="smallexample"> int a[6] = { [4] = 29, [2] = 15 }; 72</pre> 73 <p class="noindent">is equivalent to 74 75<pre class="smallexample"> int a[6] = { 0, 0, 15, 0, 29, 0 }; 76</pre> 77 <p class="noindent">The index values must be constant expressions, even if the array being 78initialized is automatic. 79 80 <p>An alternative syntax for this that has been obsolete since GCC 2.5 but 81GCC still accepts is to write ‘<samp><span class="samp">[</span><var>index</var><span class="samp">]</span></samp>’ before the element 82value, with no ‘<samp><span class="samp">=</span></samp>’. 83 84 <p>To initialize a range of elements to the same value, write 85‘<samp><span class="samp">[</span><var>first</var><span class="samp"> ... </span><var>last</var><span class="samp">] = </span><var>value</var></samp>’. This is a GNU 86extension. For example, 87 88<pre class="smallexample"> int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 }; 89</pre> 90 <p class="noindent">If the value in it has side-effects, the side-effects happen only once, 91not for each initialized field by the range initializer. 92 93<p class="noindent">Note that the length of the array is the highest value specified 94plus one. 95 96 <p>In a structure initializer, specify the name of a field to initialize 97with ‘<samp><span class="samp">.</span><var>fieldname</var><span class="samp"> =</span></samp>’ before the element value. For example, 98given the following structure, 99 100<pre class="smallexample"> struct point { int x, y; }; 101</pre> 102 <p class="noindent">the following initialization 103 104<pre class="smallexample"> struct point p = { .y = yvalue, .x = xvalue }; 105</pre> 106 <p class="noindent">is equivalent to 107 108<pre class="smallexample"> struct point p = { xvalue, yvalue }; 109</pre> 110 <p>Another syntax that has the same meaning, obsolete since GCC 2.5, is 111‘<samp><var>fieldname</var><span class="samp">:</span></samp>’, as shown here: 112 113<pre class="smallexample"> struct point p = { y: yvalue, x: xvalue }; 114</pre> 115 <p><a name="index-designators-2571"></a>The ‘<samp><span class="samp">[</span><var>index</var><span class="samp">]</span></samp>’ or ‘<samp><span class="samp">.</span><var>fieldname</var></samp>’ is known as a 116<dfn>designator</dfn>. You can also use a designator (or the obsolete colon 117syntax) when initializing a union, to specify which element of the union 118should be used. For example, 119 120<pre class="smallexample"> union foo { int i; double d; }; 121 122 union foo f = { .d = 4 }; 123</pre> 124 <p class="noindent">converts 4 to a <code>double</code> to store it in the union using 125the second element. By contrast, casting 4 to type <code>union foo</code> 126stores it into the union as the integer <code>i</code>, since it is 127an integer. (See <a href="Cast-to-Union.html#Cast-to-Union">Cast to Union</a>.) 128 129 <p>You can combine this technique of naming elements with ordinary C 130initialization of successive elements. Each initializer element that 131does not have a designator applies to the next consecutive element of the 132array or structure. For example, 133 134<pre class="smallexample"> int a[6] = { [1] = v1, v2, [4] = v4 }; 135</pre> 136 <p class="noindent">is equivalent to 137 138<pre class="smallexample"> int a[6] = { 0, v1, v2, 0, v4, 0 }; 139</pre> 140 <p>Labeling the elements of an array initializer is especially useful 141when the indices are characters or belong to an <code>enum</code> type. 142For example: 143 144<pre class="smallexample"> int whitespace[256] 145 = { [' '] = 1, ['\t'] = 1, ['\h'] = 1, 146 ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 }; 147</pre> 148 <p><a name="index-designator-lists-2572"></a>You can also write a series of ‘<samp><span class="samp">.</span><var>fieldname</var></samp>’ and 149‘<samp><span class="samp">[</span><var>index</var><span class="samp">]</span></samp>’ designators before an ‘<samp><span class="samp">=</span></samp>’ to specify a 150nested subobject to initialize; the list is taken relative to the 151subobject corresponding to the closest surrounding brace pair. For 152example, with the ‘<samp><span class="samp">struct point</span></samp>’ declaration above: 153 154<pre class="smallexample"> struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 }; 155</pre> 156 <p class="noindent">If the same field is initialized multiple times, it has the value from 157the last initialization. If any such overridden initialization has 158side-effect, it is unspecified whether the side-effect happens or not. 159Currently, GCC discards them and issues a warning. 160 161 </body></html> 162 163