Designated-Inits.html 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
  2. <html>
  3. <!-- Copyright (C) 1988-2017 Free Software Foundation, Inc.
  4. Permission is granted to copy, distribute and/or modify this document
  5. under the terms of the GNU Free Documentation License, Version 1.3 or
  6. any later version published by the Free Software Foundation; with the
  7. Invariant Sections being "Funding Free Software", the Front-Cover
  8. Texts being (a) (see below), and with the Back-Cover Texts being (b)
  9. (see below). A copy of the license is included in the section entitled
  10. "GNU Free Documentation License".
  11. (a) The FSF's Front-Cover Text is:
  12. A GNU Manual
  13. (b) The FSF's Back-Cover Text is:
  14. You have freedom to copy and modify this GNU Manual, like GNU
  15. software. Copies published by the Free Software Foundation raise
  16. funds for GNU development. -->
  17. <!-- Created by GNU Texinfo 5.2, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <title>Using the GNU Compiler Collection (GCC): Designated Inits</title>
  20. <meta name="description" content="Using the GNU Compiler Collection (GCC): Designated Inits">
  21. <meta name="keywords" content="Using the GNU Compiler Collection (GCC): Designated Inits">
  22. <meta name="resource-type" content="document">
  23. <meta name="distribution" content="global">
  24. <meta name="Generator" content="makeinfo">
  25. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  26. <link href="index.html#Top" rel="start" title="Top">
  27. <link href="Option-Index.html#Option-Index" rel="index" title="Option Index">
  28. <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
  29. <link href="C-Extensions.html#C-Extensions" rel="up" title="C Extensions">
  30. <link href="Case-Ranges.html#Case-Ranges" rel="next" title="Case Ranges">
  31. <link href="Compound-Literals.html#Compound-Literals" rel="prev" title="Compound Literals">
  32. <style type="text/css">
  33. <!--
  34. a.summary-letter {text-decoration: none}
  35. blockquote.smallquotation {font-size: smaller}
  36. div.display {margin-left: 3.2em}
  37. div.example {margin-left: 3.2em}
  38. div.indentedblock {margin-left: 3.2em}
  39. div.lisp {margin-left: 3.2em}
  40. div.smalldisplay {margin-left: 3.2em}
  41. div.smallexample {margin-left: 3.2em}
  42. div.smallindentedblock {margin-left: 3.2em; font-size: smaller}
  43. div.smalllisp {margin-left: 3.2em}
  44. kbd {font-style:oblique}
  45. pre.display {font-family: inherit}
  46. pre.format {font-family: inherit}
  47. pre.menu-comment {font-family: serif}
  48. pre.menu-preformatted {font-family: serif}
  49. pre.smalldisplay {font-family: inherit; font-size: smaller}
  50. pre.smallexample {font-size: smaller}
  51. pre.smallformat {font-family: inherit; font-size: smaller}
  52. pre.smalllisp {font-size: smaller}
  53. span.nocodebreak {white-space:nowrap}
  54. span.nolinebreak {white-space:nowrap}
  55. span.roman {font-family:serif; font-weight:normal}
  56. span.sansserif {font-family:sans-serif; font-weight:normal}
  57. ul.no-bullet {list-style: none}
  58. -->
  59. </style>
  60. </head>
  61. <body lang="en" bgcolor="#FFFFFF" text="#000000" link="#0000FF" vlink="#800080" alink="#FF0000">
  62. <a name="Designated-Inits"></a>
  63. <div class="header">
  64. <p>
  65. Next: <a href="Case-Ranges.html#Case-Ranges" accesskey="n" rel="next">Case Ranges</a>, Previous: <a href="Compound-Literals.html#Compound-Literals" accesskey="p" rel="prev">Compound Literals</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  66. </div>
  67. <hr>
  68. <a name="Designated-Initializers"></a>
  69. <h3 class="section">6.27 Designated Initializers</h3>
  70. <a name="index-initializers-with-labeled-elements"></a>
  71. <a name="index-labeled-elements-in-initializers"></a>
  72. <a name="index-case-labels-in-initializers"></a>
  73. <a name="index-designated-initializers"></a>
  74. <p>Standard C90 requires the elements of an initializer to appear in a fixed
  75. order, the same as the order of the elements in the array or structure
  76. being initialized.
  77. </p>
  78. <p>In ISO C99 you can give the elements in any order, specifying the array
  79. indices or structure field names they apply to, and GNU C allows this as
  80. an extension in C90 mode as well. This extension is not
  81. implemented in GNU C++.
  82. </p>
  83. <p>To specify an array index, write
  84. &lsquo;<samp>[<var>index</var>] =</samp>&rsquo; before the element value. For example,
  85. </p>
  86. <div class="smallexample">
  87. <pre class="smallexample">int a[6] = { [4] = 29, [2] = 15 };
  88. </pre></div>
  89. <p>is equivalent to
  90. </p>
  91. <div class="smallexample">
  92. <pre class="smallexample">int a[6] = { 0, 0, 15, 0, 29, 0 };
  93. </pre></div>
  94. <p>The index values must be constant expressions, even if the array being
  95. initialized is automatic.
  96. </p>
  97. <p>An alternative syntax for this that has been obsolete since GCC 2.5 but
  98. GCC still accepts is to write &lsquo;<samp>[<var>index</var>]</samp>&rsquo; before the element
  99. value, with no &lsquo;<samp>=</samp>&rsquo;.
  100. </p>
  101. <p>To initialize a range of elements to the same value, write
  102. &lsquo;<samp>[<var>first</var> ... <var>last</var>] = <var>value</var></samp>&rsquo;. This is a GNU
  103. extension. For example,
  104. </p>
  105. <div class="smallexample">
  106. <pre class="smallexample">int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
  107. </pre></div>
  108. <p>If the value in it has side-effects, the side-effects happen only once,
  109. not for each initialized field by the range initializer.
  110. </p>
  111. <p>Note that the length of the array is the highest value specified
  112. plus one.
  113. </p>
  114. <p>In a structure initializer, specify the name of a field to initialize
  115. with &lsquo;<samp>.<var>fieldname</var> =</samp>&rsquo; before the element value. For example,
  116. given the following structure,
  117. </p>
  118. <div class="smallexample">
  119. <pre class="smallexample">struct point { int x, y; };
  120. </pre></div>
  121. <p>the following initialization
  122. </p>
  123. <div class="smallexample">
  124. <pre class="smallexample">struct point p = { .y = yvalue, .x = xvalue };
  125. </pre></div>
  126. <p>is equivalent to
  127. </p>
  128. <div class="smallexample">
  129. <pre class="smallexample">struct point p = { xvalue, yvalue };
  130. </pre></div>
  131. <p>Another syntax that has the same meaning, obsolete since GCC 2.5, is
  132. &lsquo;<samp><var>fieldname</var>:</samp>&rsquo;, as shown here:
  133. </p>
  134. <div class="smallexample">
  135. <pre class="smallexample">struct point p = { y: yvalue, x: xvalue };
  136. </pre></div>
  137. <p>Omitted field members are implicitly initialized the same as objects
  138. that have static storage duration.
  139. </p>
  140. <a name="index-designators"></a>
  141. <p>The &lsquo;<samp>[<var>index</var>]</samp>&rsquo; or &lsquo;<samp>.<var>fieldname</var></samp>&rsquo; is known as a
  142. <em>designator</em>. You can also use a designator (or the obsolete colon
  143. syntax) when initializing a union, to specify which element of the union
  144. should be used. For example,
  145. </p>
  146. <div class="smallexample">
  147. <pre class="smallexample">union foo { int i; double d; };
  148. union foo f = { .d = 4 };
  149. </pre></div>
  150. <p>converts 4 to a <code>double</code> to store it in the union using
  151. the second element. By contrast, casting 4 to type <code>union foo</code>
  152. stores it into the union as the integer <code>i</code>, since it is
  153. an integer. See <a href="Cast-to-Union.html#Cast-to-Union">Cast to Union</a>.
  154. </p>
  155. <p>You can combine this technique of naming elements with ordinary C
  156. initialization of successive elements. Each initializer element that
  157. does not have a designator applies to the next consecutive element of the
  158. array or structure. For example,
  159. </p>
  160. <div class="smallexample">
  161. <pre class="smallexample">int a[6] = { [1] = v1, v2, [4] = v4 };
  162. </pre></div>
  163. <p>is equivalent to
  164. </p>
  165. <div class="smallexample">
  166. <pre class="smallexample">int a[6] = { 0, v1, v2, 0, v4, 0 };
  167. </pre></div>
  168. <p>Labeling the elements of an array initializer is especially useful
  169. when the indices are characters or belong to an <code>enum</code> type.
  170. For example:
  171. </p>
  172. <div class="smallexample">
  173. <pre class="smallexample">int whitespace[256]
  174. = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
  175. ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
  176. </pre></div>
  177. <a name="index-designator-lists"></a>
  178. <p>You can also write a series of &lsquo;<samp>.<var>fieldname</var></samp>&rsquo; and
  179. &lsquo;<samp>[<var>index</var>]</samp>&rsquo; designators before an &lsquo;<samp>=</samp>&rsquo; to specify a
  180. nested subobject to initialize; the list is taken relative to the
  181. subobject corresponding to the closest surrounding brace pair. For
  182. example, with the &lsquo;<samp>struct point</samp>&rsquo; declaration above:
  183. </p>
  184. <div class="smallexample">
  185. <pre class="smallexample">struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
  186. </pre></div>
  187. <p>If the same field is initialized multiple times, it has the value from
  188. the last initialization. If any such overridden initialization has
  189. side-effect, it is unspecified whether the side-effect happens or not.
  190. Currently, GCC discards them and issues a warning.
  191. </p>
  192. <hr>
  193. <div class="header">
  194. <p>
  195. Next: <a href="Case-Ranges.html#Case-Ranges" accesskey="n" rel="next">Case Ranges</a>, Previous: <a href="Compound-Literals.html#Compound-Literals" accesskey="p" rel="prev">Compound Literals</a>, Up: <a href="C-Extensions.html#C-Extensions" accesskey="u" rel="up">C Extensions</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Option-Index.html#Option-Index" title="Index" rel="index">Index</a>]</p>
  196. </div>
  197. </body>
  198. </html>