Du kannst nicht mehr als 25 Themen auswählen Themen müssen entweder mit einem Buchstaben oder einer Ziffer beginnen. Sie können Bindestriche („-“) enthalten und bis zu 35 Zeichen lang sein.

227 Zeilen
9.2KB

  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-2020 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 6.5, http://www.gnu.org/software/texinfo/ -->
  18. <head>
  19. <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  20. <title>Designated Inits (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Designated Inits (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Designated Inits (Using the GNU Compiler Collection (GCC))">
  23. <meta name="resource-type" content="document">
  24. <meta name="distribution" content="global">
  25. <meta name="Generator" content="makeinfo">
  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.indentedblock {margin-right: 0em}
  36. blockquote.smallindentedblock {margin-right: 0em; font-size: smaller}
  37. blockquote.smallquotation {font-size: smaller}
  38. div.display {margin-left: 3.2em}
  39. div.example {margin-left: 3.2em}
  40. div.lisp {margin-left: 3.2em}
  41. div.smalldisplay {margin-left: 3.2em}
  42. div.smallexample {margin-left: 3.2em}
  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.nolinebreak {white-space: nowrap}
  54. span.roman {font-family: initial; font-weight: normal}
  55. span.sansserif {font-family: sans-serif; font-weight: normal}
  56. ul.no-bullet {list-style: none}
  57. -->
  58. </style>
  59. </head>
  60. <body lang="en">
  61. <a name="Designated-Inits"></a>
  62. <div class="header">
  63. <p>
  64. 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>
  65. </div>
  66. <hr>
  67. <a name="Designated-Initializers"></a>
  68. <h3 class="section">6.29 Designated Initializers</h3>
  69. <a name="index-initializers-with-labeled-elements"></a>
  70. <a name="index-labeled-elements-in-initializers"></a>
  71. <a name="index-case-labels-in-initializers"></a>
  72. <a name="index-designated-initializers"></a>
  73. <p>Standard C90 requires the elements of an initializer to appear in a fixed
  74. order, the same as the order of the elements in the array or structure
  75. being initialized.
  76. </p>
  77. <p>In ISO C99 you can give the elements in any order, specifying the array
  78. indices or structure field names they apply to, and GNU C allows this as
  79. an extension in C90 mode as well. This extension is not
  80. implemented in GNU C++.
  81. </p>
  82. <p>To specify an array index, write
  83. &lsquo;<samp>[<var>index</var>] =</samp>&rsquo; before the element value. For example,
  84. </p>
  85. <div class="smallexample">
  86. <pre class="smallexample">int a[6] = { [4] = 29, [2] = 15 };
  87. </pre></div>
  88. <p>is equivalent to
  89. </p>
  90. <div class="smallexample">
  91. <pre class="smallexample">int a[6] = { 0, 0, 15, 0, 29, 0 };
  92. </pre></div>
  93. <p>The index values must be constant expressions, even if the array being
  94. initialized is automatic.
  95. </p>
  96. <p>An alternative syntax for this that has been obsolete since GCC 2.5 but
  97. GCC still accepts is to write &lsquo;<samp>[<var>index</var>]</samp>&rsquo; before the element
  98. value, with no &lsquo;<samp>=</samp>&rsquo;.
  99. </p>
  100. <p>To initialize a range of elements to the same value, write
  101. &lsquo;<samp>[<var>first</var> ... <var>last</var>] = <var>value</var></samp>&rsquo;. This is a GNU
  102. extension. For example,
  103. </p>
  104. <div class="smallexample">
  105. <pre class="smallexample">int widths[] = { [0 ... 9] = 1, [10 ... 99] = 2, [100] = 3 };
  106. </pre></div>
  107. <p>If the value in it has side effects, the side effects happen only once,
  108. not for each initialized field by the range initializer.
  109. </p>
  110. <p>Note that the length of the array is the highest value specified
  111. plus one.
  112. </p>
  113. <p>In a structure initializer, specify the name of a field to initialize
  114. with &lsquo;<samp>.<var>fieldname</var> =</samp>&rsquo; before the element value. For example,
  115. given the following structure,
  116. </p>
  117. <div class="smallexample">
  118. <pre class="smallexample">struct point { int x, y; };
  119. </pre></div>
  120. <p>the following initialization
  121. </p>
  122. <div class="smallexample">
  123. <pre class="smallexample">struct point p = { .y = yvalue, .x = xvalue };
  124. </pre></div>
  125. <p>is equivalent to
  126. </p>
  127. <div class="smallexample">
  128. <pre class="smallexample">struct point p = { xvalue, yvalue };
  129. </pre></div>
  130. <p>Another syntax that has the same meaning, obsolete since GCC 2.5, is
  131. &lsquo;<samp><var>fieldname</var>:</samp>&rsquo;, as shown here:
  132. </p>
  133. <div class="smallexample">
  134. <pre class="smallexample">struct point p = { y: yvalue, x: xvalue };
  135. </pre></div>
  136. <p>Omitted fields are implicitly initialized the same as for objects
  137. that have static storage duration.
  138. </p>
  139. <a name="index-designators"></a>
  140. <p>The &lsquo;<samp>[<var>index</var>]</samp>&rsquo; or &lsquo;<samp>.<var>fieldname</var></samp>&rsquo; is known as a
  141. <em>designator</em>. You can also use a designator (or the obsolete colon
  142. syntax) when initializing a union, to specify which element of the union
  143. should be used. For example,
  144. </p>
  145. <div class="smallexample">
  146. <pre class="smallexample">union foo { int i; double d; };
  147. union foo f = { .d = 4 };
  148. </pre></div>
  149. <p>converts 4 to a <code>double</code> to store it in the union using
  150. the second element. By contrast, casting 4 to type <code>union foo</code>
  151. stores it into the union as the integer <code>i</code>, since it is
  152. an integer. See <a href="Cast-to-Union.html#Cast-to-Union">Cast to Union</a>.
  153. </p>
  154. <p>You can combine this technique of naming elements with ordinary C
  155. initialization of successive elements. Each initializer element that
  156. does not have a designator applies to the next consecutive element of the
  157. array or structure. For example,
  158. </p>
  159. <div class="smallexample">
  160. <pre class="smallexample">int a[6] = { [1] = v1, v2, [4] = v4 };
  161. </pre></div>
  162. <p>is equivalent to
  163. </p>
  164. <div class="smallexample">
  165. <pre class="smallexample">int a[6] = { 0, v1, v2, 0, v4, 0 };
  166. </pre></div>
  167. <p>Labeling the elements of an array initializer is especially useful
  168. when the indices are characters or belong to an <code>enum</code> type.
  169. For example:
  170. </p>
  171. <div class="smallexample">
  172. <pre class="smallexample">int whitespace[256]
  173. = { [' '] = 1, ['\t'] = 1, ['\h'] = 1,
  174. ['\f'] = 1, ['\n'] = 1, ['\r'] = 1 };
  175. </pre></div>
  176. <a name="index-designator-lists"></a>
  177. <p>You can also write a series of &lsquo;<samp>.<var>fieldname</var></samp>&rsquo; and
  178. &lsquo;<samp>[<var>index</var>]</samp>&rsquo; designators before an &lsquo;<samp>=</samp>&rsquo; to specify a
  179. nested subobject to initialize; the list is taken relative to the
  180. subobject corresponding to the closest surrounding brace pair. For
  181. example, with the &lsquo;<samp>struct point</samp>&rsquo; declaration above:
  182. </p>
  183. <div class="smallexample">
  184. <pre class="smallexample">struct point ptarray[10] = { [2].y = yv2, [2].x = xv2, [0].x = xv0 };
  185. </pre></div>
  186. <p>If the same field is initialized multiple times, or overlapping
  187. fields of a union are initialized, the value from the last
  188. initialization is used. When a field of a union is itself a structure,
  189. the entire structure from the last field initialized is used. If any previous
  190. initializer has side effect, it is unspecified whether the side effect
  191. happens or not. Currently, GCC discards the side-effecting
  192. initializer expressions and issues a warning.
  193. </p>
  194. <hr>
  195. <div class="header">
  196. <p>
  197. 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>
  198. </div>
  199. </body>
  200. </html>