Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

164 lines
6.6KB

  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>Unnamed Fields (Using the GNU Compiler Collection (GCC))</title>
  21. <meta name="description" content="Unnamed Fields (Using the GNU Compiler Collection (GCC))">
  22. <meta name="keywords" content="Unnamed Fields (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="Thread_002dLocal.html#Thread_002dLocal" rel="next" title="Thread-Local">
  31. <link href="Loop_002dSpecific-Pragmas.html#Loop_002dSpecific-Pragmas" rel="prev" title="Loop-Specific Pragmas">
  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="Unnamed-Fields"></a>
  62. <div class="header">
  63. <p>
  64. Next: <a href="Thread_002dLocal.html#Thread_002dLocal" accesskey="n" rel="next">Thread-Local</a>, Previous: <a href="Pragmas.html#Pragmas" accesskey="p" rel="prev">Pragmas</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="Unnamed-Structure-and-Union-Fields"></a>
  68. <h3 class="section">6.63 Unnamed Structure and Union Fields</h3>
  69. <a name="index-struct"></a>
  70. <a name="index-union"></a>
  71. <p>As permitted by ISO C11 and for compatibility with other compilers,
  72. GCC allows you to define
  73. a structure or union that contains, as fields, structures and unions
  74. without names. For example:
  75. </p>
  76. <div class="smallexample">
  77. <pre class="smallexample">struct {
  78. int a;
  79. union {
  80. int b;
  81. float c;
  82. };
  83. int d;
  84. } foo;
  85. </pre></div>
  86. <p>In this example, you are able to access members of the unnamed
  87. union with code like &lsquo;<samp>foo.b</samp>&rsquo;. Note that only unnamed structs and
  88. unions are allowed, you may not have, for example, an unnamed
  89. <code>int</code>.
  90. </p>
  91. <p>You must never create such structures that cause ambiguous field definitions.
  92. For example, in this structure:
  93. </p>
  94. <div class="smallexample">
  95. <pre class="smallexample">struct {
  96. int a;
  97. struct {
  98. int a;
  99. };
  100. } foo;
  101. </pre></div>
  102. <p>it is ambiguous which <code>a</code> is being referred to with &lsquo;<samp>foo.a</samp>&rsquo;.
  103. The compiler gives errors for such constructs.
  104. </p>
  105. <a name="index-fms_002dextensions-2"></a>
  106. <p>Unless <samp>-fms-extensions</samp> is used, the unnamed field must be a
  107. structure or union definition without a tag (for example, &lsquo;<samp>struct
  108. { int a; };</samp>&rsquo;). If <samp>-fms-extensions</samp> is used, the field may
  109. also be a definition with a tag such as &lsquo;<samp>struct foo { int a;
  110. };</samp>&rsquo;, a reference to a previously defined structure or union such as
  111. &lsquo;<samp>struct foo;</samp>&rsquo;, or a reference to a <code>typedef</code> name for a
  112. previously defined structure or union type.
  113. </p>
  114. <a name="index-fplan9_002dextensions-1"></a>
  115. <p>The option <samp>-fplan9-extensions</samp> enables
  116. <samp>-fms-extensions</samp> as well as two other extensions. First, a
  117. pointer to a structure is automatically converted to a pointer to an
  118. anonymous field for assignments and function calls. For example:
  119. </p>
  120. <div class="smallexample">
  121. <pre class="smallexample">struct s1 { int a; };
  122. struct s2 { struct s1; };
  123. extern void f1 (struct s1 *);
  124. void f2 (struct s2 *p) { f1 (p); }
  125. </pre></div>
  126. <p>In the call to <code>f1</code> inside <code>f2</code>, the pointer <code>p</code> is
  127. converted into a pointer to the anonymous field.
  128. </p>
  129. <p>Second, when the type of an anonymous field is a <code>typedef</code> for a
  130. <code>struct</code> or <code>union</code>, code may refer to the field using the
  131. name of the <code>typedef</code>.
  132. </p>
  133. <div class="smallexample">
  134. <pre class="smallexample">typedef struct { int a; } s1;
  135. struct s2 { s1; };
  136. s1 f1 (struct s2 *p) { return p-&gt;s1; }
  137. </pre></div>
  138. <p>These usages are only permitted when they are not ambiguous.
  139. </p>
  140. <hr>
  141. <div class="header">
  142. <p>
  143. Next: <a href="Thread_002dLocal.html#Thread_002dLocal" accesskey="n" rel="next">Thread-Local</a>, Previous: <a href="Pragmas.html#Pragmas" accesskey="p" rel="prev">Pragmas</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>
  144. </div>
  145. </body>
  146. </html>