Unnamed-Fields.html 6.6 KB

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