You cannot select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
123 lines
5.2 KiB
HTML
123 lines
5.2 KiB
HTML
<html lang="en">
|
|
<head>
|
|
<title>Unnamed Fields - Using the GNU Compiler Collection (GCC)</title>
|
|
<meta http-equiv="Content-Type" content="text/html">
|
|
<meta name="description" content="Using the GNU Compiler Collection (GCC)">
|
|
<meta name="generator" content="makeinfo 4.13">
|
|
<link title="Top" rel="start" href="index.html#Top">
|
|
<link rel="up" href="C-Extensions.html#C-Extensions" title="C Extensions">
|
|
<link rel="prev" href="Pragmas.html#Pragmas" title="Pragmas">
|
|
<link rel="next" href="Thread_002dLocal.html#Thread_002dLocal" title="Thread-Local">
|
|
<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage">
|
|
<!--
|
|
Copyright (C) 1988-2015 Free Software Foundation, Inc.
|
|
|
|
Permission is granted to copy, distribute and/or modify this document
|
|
under the terms of the GNU Free Documentation License, Version 1.3 or
|
|
any later version published by the Free Software Foundation; with the
|
|
Invariant Sections being ``Funding Free Software'', the Front-Cover
|
|
Texts being (a) (see below), and with the Back-Cover Texts being (b)
|
|
(see below). A copy of the license is included in the section entitled
|
|
``GNU Free Documentation License''.
|
|
|
|
(a) The FSF's Front-Cover Text is:
|
|
|
|
A GNU Manual
|
|
|
|
(b) The FSF's Back-Cover Text is:
|
|
|
|
You have freedom to copy and modify this GNU Manual, like GNU
|
|
software. Copies published by the Free Software Foundation raise
|
|
funds for GNU development.-->
|
|
<meta http-equiv="Content-Style-Type" content="text/css">
|
|
<style type="text/css"><!--
|
|
pre.display { font-family:inherit }
|
|
pre.format { font-family:inherit }
|
|
pre.smalldisplay { font-family:inherit; font-size:smaller }
|
|
pre.smallformat { font-family:inherit; font-size:smaller }
|
|
pre.smallexample { font-size:smaller }
|
|
pre.smalllisp { font-size:smaller }
|
|
span.sc { font-variant:small-caps }
|
|
span.roman { font-family:serif; font-weight:normal; }
|
|
span.sansserif { font-family:sans-serif; font-weight:normal; }
|
|
--></style>
|
|
</head>
|
|
<body>
|
|
<div class="node">
|
|
<a name="Unnamed-Fields"></a>
|
|
<p>
|
|
Next: <a rel="next" accesskey="n" href="Thread_002dLocal.html#Thread_002dLocal">Thread-Local</a>,
|
|
Previous: <a rel="previous" accesskey="p" href="Pragmas.html#Pragmas">Pragmas</a>,
|
|
Up: <a rel="up" accesskey="u" href="C-Extensions.html#C-Extensions">C Extensions</a>
|
|
<hr>
|
|
</div>
|
|
|
|
<h3 class="section">6.61 Unnamed Structure and Union Fields</h3>
|
|
|
|
<p><a name="index-g_t_0040code_007bstruct_007d-4189"></a><a name="index-g_t_0040code_007bunion_007d-4190"></a>
|
|
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:
|
|
|
|
<pre class="smallexample"> struct {
|
|
int a;
|
|
union {
|
|
int b;
|
|
float c;
|
|
};
|
|
int d;
|
|
} foo;
|
|
</pre>
|
|
<p class="noindent">In this example, you are able to access members of the unnamed
|
|
union with code like ‘<samp><span class="samp">foo.b</span></samp>’. Note that only unnamed structs and
|
|
unions are allowed, you may not have, for example, an unnamed
|
|
<code>int</code>.
|
|
|
|
<p>You must never create such structures that cause ambiguous field definitions.
|
|
For example, in this structure:
|
|
|
|
<pre class="smallexample"> struct {
|
|
int a;
|
|
struct {
|
|
int a;
|
|
};
|
|
} foo;
|
|
</pre>
|
|
<p class="noindent">it is ambiguous which <code>a</code> is being referred to with ‘<samp><span class="samp">foo.a</span></samp>’.
|
|
The compiler gives errors for such constructs.
|
|
|
|
<p><a name="index-fms_002dextensions-4191"></a>Unless <samp><span class="option">-fms-extensions</span></samp> is used, the unnamed field must be a
|
|
structure or union definition without a tag (for example, ‘<samp><span class="samp">struct
|
|
{ int a; };</span></samp>’). If <samp><span class="option">-fms-extensions</span></samp> is used, the field may
|
|
also be a definition with a tag such as ‘<samp><span class="samp">struct foo { int a;
|
|
};</span></samp>’, a reference to a previously defined structure or union such as
|
|
‘<samp><span class="samp">struct foo;</span></samp>’, or a reference to a <code>typedef</code> name for a
|
|
previously defined structure or union type.
|
|
|
|
<p><a name="index-fplan9_002dextensions-4192"></a>The option <samp><span class="option">-fplan9-extensions</span></samp> enables
|
|
<samp><span class="option">-fms-extensions</span></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:
|
|
|
|
<pre class="smallexample"> struct s1 { int a; };
|
|
struct s2 { struct s1; };
|
|
extern void f1 (struct s1 *);
|
|
void f2 (struct s2 *p) { f1 (p); }
|
|
</pre>
|
|
<p class="noindent">In the call to <code>f1</code> inside <code>f2</code>, the pointer <code>p</code> is
|
|
converted into a pointer to the anonymous field.
|
|
|
|
<p>Second, when the type of an anonymous field is a <code>typedef</code> for a
|
|
<code>struct</code> or <code>union</code>, code may refer to the field using the
|
|
name of the <code>typedef</code>.
|
|
|
|
<pre class="smallexample"> typedef struct { int a; } s1;
|
|
struct s2 { s1; };
|
|
s1 f1 (struct s2 *p) { return p->s1; }
|
|
</pre>
|
|
<p>These usages are only permitted when they are not ambiguous.
|
|
|
|
</body></html>
|
|
|