1<html lang="en"> 2<head> 3<title>Unnamed Fields - Using the GNU Compiler Collection (GCC)</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Using the GNU Compiler Collection (GCC)"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions"> 9<link rel="prev" href="Pragmas.html#Pragmas" title="Pragmas"> 10<link rel="next" href="Thread_002dLocal.html#Thread_002dLocal" title="Thread-Local"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<!-- 13Copyright (C) 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1997, 141998, 1999, 2000, 2001, 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 152010 Free Software Foundation, Inc. 16 17Permission is granted to copy, distribute and/or modify this document 18under the terms of the GNU Free Documentation License, Version 1.3 or 19any later version published by the Free Software Foundation; with the 20Invariant Sections being ``Funding Free Software'', the Front-Cover 21Texts being (a) (see below), and with the Back-Cover Texts being (b) 22(see below). A copy of the license is included in the section entitled 23``GNU Free Documentation License''. 24 25(a) The FSF's Front-Cover Text is: 26 27 A GNU Manual 28 29(b) The FSF's Back-Cover Text is: 30 31 You have freedom to copy and modify this GNU Manual, like GNU 32 software. Copies published by the Free Software Foundation raise 33 funds for GNU development.--> 34<meta http-equiv="Content-Style-Type" content="text/css"> 35<style type="text/css"><!-- 36 pre.display { font-family:inherit } 37 pre.format { font-family:inherit } 38 pre.smalldisplay { font-family:inherit; font-size:smaller } 39 pre.smallformat { font-family:inherit; font-size:smaller } 40 pre.smallexample { font-size:smaller } 41 pre.smalllisp { font-size:smaller } 42 span.sc { font-variant:small-caps } 43 span.roman { font-family:serif; font-weight:normal; } 44 span.sansserif { font-family:sans-serif; font-weight:normal; } 45--></style> 46<link rel="stylesheet" type="text/css" href="../cs.css"> 47</head> 48<body> 49<div class="node"> 50<a name="Unnamed-Fields"></a> 51<p> 52Next: <a rel="next" accesskey="n" href="Thread_002dLocal.html#Thread_002dLocal">Thread-Local</a>, 53Previous: <a rel="previous" accesskey="p" href="Pragmas.html#Pragmas">Pragmas</a>, 54Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a> 55<hr> 56</div> 57 58<h3 class="section">6.57 Unnamed struct/union fields within structs/unions</h3> 59 60<p><a name="index-g_t_0040code_007bstruct_007d-3245"></a><a name="index-g_t_0040code_007bunion_007d-3246"></a> 61As permitted by ISO C1X and for compatibility with other compilers, 62GCC allows you to define 63a structure or union that contains, as fields, structures and unions 64without names. For example: 65 66<pre class="smallexample"> struct { 67 int a; 68 union { 69 int b; 70 float c; 71 }; 72 int d; 73 } foo; 74</pre> 75 <p>In this example, the user would be able to access members of the unnamed 76union with code like ‘<samp><span class="samp">foo.b</span></samp>’. Note that only unnamed structs and 77unions are allowed, you may not have, for example, an unnamed 78<code>int</code>. 79 80 <p>You must never create such structures that cause ambiguous field definitions. 81For example, this structure: 82 83<pre class="smallexample"> struct { 84 int a; 85 struct { 86 int a; 87 }; 88 } foo; 89</pre> 90 <p>It is ambiguous which <code>a</code> is being referred to with ‘<samp><span class="samp">foo.a</span></samp>’. 91The compiler gives errors for such constructs. 92 93 <p><a name="index-fms_002dextensions-3247"></a>Unless <samp><span class="option">-fms-extensions</span></samp> is used, the unnamed field must be a 94structure or union definition without a tag (for example, ‘<samp><span class="samp">struct 95{ int a; };</span></samp>’). If <samp><span class="option">-fms-extensions</span></samp> is used, the field may 96also be a definition with a tag such as ‘<samp><span class="samp">struct foo { int a; 97};</span></samp>’, a reference to a previously defined structure or union such as 98‘<samp><span class="samp">struct foo;</span></samp>’, or a reference to a <code>typedef</code> name for a 99previously defined structure or union type. 100 101 <p><a name="index-fplan9_002dextensions-3248"></a>The option <samp><span class="option">-fplan9-extensions</span></samp> enables 102<samp><span class="option">-fms-extensions</span></samp> as well as two other extensions. First, a 103pointer to a structure is automatically converted to a pointer to an 104anonymous field for assignments and function calls. For example: 105 106<pre class="smallexample"> struct s1 { int a; }; 107 struct s2 { struct s1; }; 108 extern void f1 (struct s1 *); 109 void f2 (struct s2 *p) { f1 (p); } 110</pre> 111 <p>In the call to <code>f1</code> inside <code>f2</code>, the pointer <code>p</code> is 112converted into a pointer to the anonymous field. 113 114 <p>Second, when the type of an anonymous field is a <code>typedef</code> for a 115<code>struct</code> or <code>union</code>, code may refer to the field using the 116name of the <code>typedef</code>. 117 118<pre class="smallexample"> typedef struct { int a; } s1; 119 struct s2 { s1; }; 120 s1 f1 (struct s2 *p) { return p->s1; } 121</pre> 122 <p>These usages are only permitted when they are not ambiguous. 123 124 </body></html> 125 126