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