diff options
Diffstat (limited to 'devdocs/gcc~13/unnamed-fields.html')
| -rw-r--r-- | devdocs/gcc~13/unnamed-fields.html | 27 |
1 files changed, 27 insertions, 0 deletions
diff --git a/devdocs/gcc~13/unnamed-fields.html b/devdocs/gcc~13/unnamed-fields.html new file mode 100644 index 00000000..807cee8b --- /dev/null +++ b/devdocs/gcc~13/unnamed-fields.html @@ -0,0 +1,27 @@ +<div class="section-level-extent" id="Unnamed-Fields"> <div class="nav-panel"> <p> Next: <a href="thread-local" accesskey="n" rel="next">Thread-Local Storage</a>, Previous: <a href="pragmas" accesskey="p" rel="prev">Pragmas Accepted by GCC</a>, Up: <a href="c-extensions" accesskey="u" rel="up">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div> <h1 class="section" id="Unnamed-Structure-and-Union-Fields"><span>6.63 Unnamed Structure and Union Fields<a class="copiable-link" href="#Unnamed-Structure-and-Union-Fields"> ¶</a></span></h1> <p>As permitted by ISO C11 and for compatibility with other compilers, GCC allows you to define a structure or union that contains, as fields, structures and unions without names. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct { + int a; + union { + int b; + float c; + }; + int d; +} foo;</pre> +</div> <p>In this example, you are able to access members of the unnamed union with code like ‘<samp class="samp">foo.b</samp>’. Note that only unnamed structs and unions are allowed, you may not have, for example, an unnamed <code class="code">int</code>. </p> <p>You must never create such structures that cause ambiguous field definitions. For example, in this structure: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct { + int a; + struct { + int a; + }; +} foo;</pre> +</div> <p>it is ambiguous which <code class="code">a</code> is being referred to with ‘<samp class="samp">foo.a</samp>’. The compiler gives errors for such constructs. </p> <p>Unless <samp class="option">-fms-extensions</samp> is used, the unnamed field must be a structure or union definition without a tag (for example, ‘<samp class="samp">struct { int a; };</samp>’). If <samp class="option">-fms-extensions</samp> is used, the field may also be a definition with a tag such as ‘<samp class="samp">struct foo { int a; };</samp>’, a reference to a previously defined structure or union such as ‘<samp class="samp">struct foo;</samp>’, or a reference to a <code class="code">typedef</code> name for a previously defined structure or union type. </p> <p>The option <samp class="option">-fplan9-extensions</samp> enables <samp class="option">-fms-extensions</samp> as well as two other extensions. First, a pointer to a structure is automatically converted to a pointer to an anonymous field for assignments and function calls. For example: </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">struct s1 { int a; }; +struct s2 { struct s1; }; +extern void f1 (struct s1 *); +void f2 (struct s2 *p) { f1 (p); }</pre> +</div> <p>In the call to <code class="code">f1</code> inside <code class="code">f2</code>, the pointer <code class="code">p</code> is converted into a pointer to the anonymous field. </p> <p>Second, when the type of an anonymous field is a <code class="code">typedef</code> for a <code class="code">struct</code> or <code class="code">union</code>, code may refer to the field using the name of the <code class="code">typedef</code>. </p> <div class="example smallexample"> <pre class="example-preformatted" data-language="cpp">typedef struct { int a; } s1; +struct s2 { s1; }; +s1 f1 (struct s2 *p) { return p->s1; }</pre> +</div> <p>These usages are only permitted when they are not ambiguous. </p> </div> <div class="nav-panel"> <p> Next: <a href="thread-local">Thread-Local Storage</a>, Previous: <a href="pragmas">Pragmas Accepted by GCC</a>, Up: <a href="c-extensions">Extensions to the C Language Family</a> [<a href="index#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="indices" title="Index" rel="index">Index</a>]</p> </div><div class="_attribution"> + <p class="_attribution-p"> + © Free Software Foundation<br>Licensed under the GNU Free Documentation License, Version 1.3.<br> + <a href="https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Unnamed-Fields.html" class="_attribution-link">https://gcc.gnu.org/onlinedocs/gcc-13.1.0/gcc/Unnamed-Fields.html</a> + </p> +</div> |
