source: public/doc/gnu-c/Structures.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 11.1 KB
Line 
1<!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
2<html>
3<!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
4
5(The work of Trevis Rothwell and Nelson Beebe has been assigned or
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing GNU and promoting software freedom." -->
19<!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
20<head>
21<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
22<title>Structures (GNU C Language Manual)</title>
23
24<meta name="description" content="Structures (GNU C Language Manual)">
25<meta name="keywords" content="Structures (GNU C Language Manual)">
26<meta name="resource-type" content="document">
27<meta name="distribution" content="global">
28<meta name="Generator" content="makeinfo">
29<link href="index.html" rel="start" title="Top">
30<link href="Symbol-Index.html" rel="index" title="Symbol Index">
31<link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
32<link href="index.html" rel="up" title="Top">
33<link href="Referencing-Fields.html" rel="next" title="Referencing Fields">
34<link href="Printing-Pointers.html" rel="prev" title="Printing Pointers">
35<style type="text/css">
36<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.no-bullet {list-style: none}
51-->
52</style>
53
54
55</head>
56
57<body lang="en">
58<span id="Structures"></span><div class="header">
59<p>
60Next: <a href="Arrays.html" accesskey="n" rel="next">Arrays</a>, Previous: <a href="Pointers.html" accesskey="p" rel="prev">Pointers</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
61</div>
62<hr>
63<span id="Structures-1"></span><h2 class="chapter">15 Structures</h2>
64<span id="index-structures"></span>
65<span id="index-struct"></span>
66<span id="index-fields-in-structures"></span>
67
68<p>A <em>structure</em> is a user-defined data type that holds various
69<em>fields</em> of data. Each field has a name and a data type specified
70in the structure&rsquo;s definition.
71</p>
72<p>Here we define a structure suitable for storing a linked list of
73integers. Each list item will hold one integer, plus a pointer
74to the next item.
75</p>
76<div class="example">
77<pre class="example">struct intlistlink
78 {
79 int datum;
80 struct intlistlink *next;
81 };
82</pre></div>
83
84<p>The structure definition has a <em>type tag</em> so that the code can
85refer to this structure. The type tag here is <code>intlistlink</code>.
86The definition refers recursively to the same structure through that
87tag.
88</p>
89<p>You can define a structure without a type tag, but then you can&rsquo;t
90refer to it again. That is useful only in some special contexts, such
91as inside a <code>typedef</code> or a <code>union</code>.
92</p>
93<p>The contents of the structure are specified by the <em>field
94declarations</em> inside the braces. Each field in the structure needs a
95declaration there. The fields in one structure definition must have
96distinct names, but these names do not conflict with any other names
97in the program.
98</p>
99<p>A field declaration looks just like a variable declaration. You can
100combine field declarations with the same beginning, just as you can
101combine variable declarations.
102</p>
103<p>This structure has two fields. One, named <code>datum</code>, has type
104<code>int</code> and will hold one integer in the list. The other, named
105<code>next</code>, is a pointer to another <code>struct intlistlink</code>
106which would be the rest of the list. In the last list item, it would
107be <code>NULL</code>.
108</p>
109<p>This structure definition is recursive, since the type of the
110<code>next</code> field refers to the structure type. Such recursion is not
111a problem; in fact, you can use the type <code>struct intlistlink *</code>
112before the definition of the type <code>struct intlistlink</code> itself.
113That works because pointers to all kinds of structures really look the
114same at the machine level.
115</p>
116<p>After defining the structure, you can declare a variable of type
117<code>struct intlistlink</code> like this:
118</p>
119<div class="example">
120<pre class="example">struct intlistlink foo;
121</pre></div>
122
123<p>The structure definition itself can serve as the beginning of a
124variable declaration, so you can declare variables immediately after,
125like this:
126</p>
127<div class="example">
128<pre class="example">struct intlistlink
129 {
130 int datum;
131 struct intlistlink *next;
132 } foo;
133</pre></div>
134
135<p>But that is ugly. It is almost always clearer to separate the
136definition of the structure from its uses.
137</p>
138<p>Declaring a structure type inside a block (see <a href="Blocks.html">Blocks</a>) limits
139the scope of the structure type name to that block. That means the
140structure type is recognized only within that block. Declaring it in
141a function parameter list, as here,
142</p>
143<div class="example">
144<pre class="example">int f (struct foo {int a, b} parm);
145</pre></div>
146
147<p>(assuming that <code>struct foo</code> is not already defined) limits the
148scope of the structure type <code>struct foo</code> to that parameter list;
149that is basically useless, so it triggers a warning.
150</p>
151<p>Standard C requires at least one field in a structure.
152GNU C does not require this.
153</p>
154<table class="menu" border="0" cellspacing="0">
155<tr><td align="left" valign="top">&bull; <a href="Referencing-Fields.html" accesskey="1">Referencing Fields</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Accessing field values in a structure object.
156</td></tr>
157<tr><td align="left" valign="top">&bull; <a href="Dynamic-Memory-Allocation.html" accesskey="2">Dynamic Memory Allocation</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Allocating space for objects
158 while the program is running.
159</td></tr>
160<tr><td align="left" valign="top">&bull; <a href="Field-Offset.html" accesskey="3">Field Offset</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Memory layout of fields within a structure.
161</td></tr>
162<tr><td align="left" valign="top">&bull; <a href="Structure-Layout.html" accesskey="4">Structure Layout</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Planning the memory layout of fields.
163</td></tr>
164<tr><td align="left" valign="top">&bull; <a href="Packed-Structures.html" accesskey="5">Packed Structures</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Packing structure fields as close as possible.
165</td></tr>
166<tr><td align="left" valign="top">&bull; <a href="Bit-Fields.html" accesskey="6">Bit Fields</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Dividing integer fields
167 into fields with fewer bits.
168</td></tr>
169<tr><td align="left" valign="top">&bull; <a href="Bit-Field-Packing.html" accesskey="7">Bit Field Packing</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">How bit fields pack together in integers.
170</td></tr>
171<tr><td align="left" valign="top">&bull; <a href="const-Fields.html" accesskey="8">const Fields</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Making structure fields immutable.
172</td></tr>
173<tr><td align="left" valign="top">&bull; <a href="Zero-Length.html" accesskey="9">Zero Length</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Zero-length array as a variable-length object.
174</td></tr>
175<tr><td align="left" valign="top">&bull; <a href="Flexible-Array-Fields.html">Flexible Array Fields</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Another approach to variable-length objects.
176</td></tr>
177<tr><td align="left" valign="top">&bull; <a href="Overlaying-Structures.html">Overlaying Structures</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Casting one structure type
178 over an object of another structure type.
179</td></tr>
180<tr><td align="left" valign="top">&bull; <a href="Structure-Assignment.html">Structure Assignment</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Assigning values to structure objects.
181</td></tr>
182<tr><td align="left" valign="top">&bull; <a href="Unions.html">Unions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Viewing the same object in different types.
183</td></tr>
184<tr><td align="left" valign="top">&bull; <a href="Packing-With-Unions.html">Packing With Unions</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Using a union type to pack various types into
185 the same memory space.
186</td></tr>
187<tr><td align="left" valign="top">&bull; <a href="Cast-to-Union.html">Cast to Union</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Casting a value one of the union&rsquo;s alternative
188 types to the type of the union itself.
189</td></tr>
190<tr><td align="left" valign="top">&bull; <a href="Structure-Constructors.html">Structure Constructors</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Building new structure objects.
191</td></tr>
192<tr><td align="left" valign="top">&bull; <a href="Unnamed-Types-as-Fields.html">Unnamed Types as Fields</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Fields&rsquo; types do not always need names.
193</td></tr>
194<tr><td align="left" valign="top">&bull; <a href="Incomplete-Types.html">Incomplete Types</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Types which have not been fully defined.
195</td></tr>
196<tr><td align="left" valign="top">&bull; <a href="Intertwined-Incomplete-Types.html">Intertwined Incomplete Types</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Defining mutually-recursive structue types.
197</td></tr>
198<tr><td align="left" valign="top">&bull; <a href="Type-Tags.html">Type Tags</a></td><td>&nbsp;&nbsp;</td><td align="left" valign="top">Scope of structure and union type tags.
199</td></tr>
200</table>
201
202<hr>
203<div class="header">
204<p>
205Next: <a href="Arrays.html" accesskey="n" rel="next">Arrays</a>, Previous: <a href="Pointers.html" accesskey="p" rel="prev">Pointers</a>, Up: <a href="index.html" accesskey="u" rel="up">Top</a> &nbsp; [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
206</div>
207
208
209
210</body>
211</html>
Note: See TracBrowser for help on using the repository browser.