* remove "\r" nonsense
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx1a.html
blob9e59805f1ed6426352aff5c7ecfe5bb99bff4815
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 by Steve Summit. -->
3 <!-- This material may be freely redistributed and used -->
4 <!-- but may not be republished or sold without permission. -->
5 <html>
6 <head>
7 <link rev="owner" href="mailto:scs@eskimo.com">
8 <link rev="made" href="mailto:scs@eskimo.com">
9 <title>15.1: Structures</title>
10 <link href="sx1.html" rev=precedes>
11 <link href="sx1b.html" rel=precedes>
12 <link href="sx1.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>15.1: Structures</H2>
17 <p>[This section corresponds to K&amp;R Sec. 6.1]
18 </p><p>The basic user-defined data type in C is the <dfn>structure</dfn>,
19 or <TT>struct</TT>.
20 (C structures are analogous to the <dfn>records</dfn>
21 found in some other languages.)
22 Defining structures is a two-step process:
23 first you define a ``template'' which describes the new type,
24 then you declare variables having the new type
25 (or functions returning the new type, etc.).
26 </p><p>As a simple example,
27 suppose we wanted to define our own type for representing complex numbers.
28 (If you're blissfully ignorant of these beasts,
29 a complex number consists of
30 a ``real'' and ``imaginary'' part,
31 where the imaginary part is some multiple of the square root of negative 1.
32 You don't have to understand complex numbers to understand this example;
33 you can think of the real and imaginary parts
34 as the <I>x</I> and <I>y</I> coordinates of a point on a plane.)
35 FORTRAN has a built-in complex type, but C does not.
37 How might we add one?
38 Since a complex number consists of a real and imaginary part,
39 we need a way of holding both these quantities in one data type,
40 and a structure will do just the trick.
41 Here is how we might declare our complex type:
42 <pre>
43 struct complex
45 double real;
46 double imag;
48 </pre>
49 A structure declaration consists of
50 up to
51 four parts,
52 of which we can see three in the example above.
53 The first part is the keyword <TT>struct</TT>
54 which indicates that we are talking about a structure.
55 The second part is a name or <dfn>tag</dfn>
56 by which this structure
57 (that is, this new data type)
58 will be known.
59 The third part is a list of the structure's <dfn>members</dfn>
60 (also called <dfn>components</dfn> or <dfn>fields</dfn>).
61 This list is enclosed in braces <TT>{}</TT>,
62 and contains what look like the declarations of ordinary variables.
63 Each member has a name and a type,
64 just like ordinary variables,
65 but here we are not declaring variables;
66 we are setting up the structure of the structure
68 by defining the collection of data types which will make up the structure.
69 Here we see that the <TT>complex</TT> structure
70 will be made up of two members,
71 both of type <TT>double</TT>,
72 one named <TT>real</TT> and one named <TT>imag</TT>.
73 </p><p>It's important to understand that what we've defined here
74 is just the new data type;
75 we have <em>not</em> yet declared any variables of this new type!
76 The name <TT>complex</TT>
77 (the second part of the structure declaration)
78 is not the name of a variable;
79 it's the name of the structure type.
80 The names <TT>real</TT> and <TT>imag</TT> are not the names of variables;
81 they're identifiers for the two components of the structure.
82 </p><p>We declare variables of our new <TT>complex</TT> type
83 with declarations like these:
84 <pre>
85 struct complex c1;
87 struct complex c2, c3;
88 </pre>
89 These look almost like our previous declarations
90 of variables having basic types,
91 except that instead of a type keyword
92 like <TT>int</TT> or <TT>double</TT>,
93 we have the two-word type name <TT>struct complex</TT>.
94 The keyword <TT>struct</TT> indicates
95 that we're talking about a structure,
96 and the identifier <TT>complex</TT>
97 is the name
98 for the particular structure we're talking about.
99 <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT> will all be declared
100 as variables of type <TT>struct complex</TT>;
101 each one of them will have real and imaginary parts buried inside them.
102 (We'll see how to get at those parts in the next section.)
103 Using our graphic, ``labeled box'' notation,
104 we could draw representations of
105 <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT>
106 like this:
107 <br>
108 <center><img src="fig15.1.gif"></center>
109 <br>
110 Actually, these pictures are a bit misleading;
111 the outer box indicating each composite structure
112 suggests that there might be more inside them
113 than just the two members,
114 <TT>real</TT> and <TT>imag</TT>
115 (that is, more than the two values of type <TT>double</TT>).
116 A simpler but more representative picture would be:
117 <br>
118 <center><img src="fig15.2.gif"></center>
119 <br>
120 The only memory allocated is for two values of type <TT>double</TT>
121 (the two boxes);
124 names
125 are just for our convenience
126 and the compiler's reference;
127 none are typically stored
128 in the program's memory
129 at run time.
130 </p><p>Notice that when we define structures in this way
131 we have not quite defined a new type
132 on a par with <TT>int</TT> or <TT>double</TT>.
133 We can <em>not</em> say
134 <pre>
135 complex c1; /* WRONG */
136 </pre>
137 The name <TT>complex</TT> does not become a full-fledged type name
138 like <TT>int</TT> or <TT>double</TT>;
139 it's just the name of a particular structure,
140 and we must use the keyword <TT>struct</TT>
141 <em>and</em> the name of a particular structure
142 (e.g. <TT>complex</TT>)
143 to talk about that structure type.
144 (There is a way to define new full-fledged type names
145 like <TT>int</TT> and <TT>double</TT>,
146 and in C++ a new structure does automatically become a full-fledged type,
147 but we won't worry about these wrinkles for now.)
148 </p><p>I said that a structure definition consisted of
149 up to
150 four parts.
151 We saw the first three of them in the first example;
152 the fourth
153 part
154 of a full strucure declaration
155 is simply a list of variables,
156 which are to be declared as having the structure type
158 at the same time as the structure itself is defined.
159 For example, if we had written
160 <pre>
161 struct complex
163 double real;
164 double imag;
165 } c1, c2, c3;
166 </pre>
167 we would have defined the type <TT>struct complex</TT>,
168 and right away declared three variables
169 <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT>
170 all of type <TT>struct complex</TT>.
171 </p><p>In fact, three of the four parts of a structure declaration
172 (all but the keyword <TT>struct</TT>)
174 are optional.
175 If a declaration contains the keyword <TT>struct</TT>,
176 a structure tag,
177 and a brace-enclosed list of members
178 (as in the first structure definition we saw),
179 it's a definition of the structure itself
180 (that is, just the template).
181 If a declaration contains the keyword <TT>struct</TT>,
182 a structure tag,
183 and a list of variable names
184 (as in the first declarations of
185 <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT>
186 we saw),
187 it's a declaration of those variables having that structure type
188 (the structure type itself must of course typically be declared elsewhere).
189 If a declaration contains all four elements
190 (as in the second declaration of
191 <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT>
192 we saw),
193 it's a definition of the structure type <em>and</em>
194 a declaration of some variables.
195 It's also possible to use the first, third, and fourth parts:
196 <pre>
197 struct {
198 double real;
199 double imag;
200 } c1, c2, c3;
201 </pre>
202 Here we declare <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT>
203 as having a structure type with no tag name,
204 which is not usually very useful,
205 because without a tag name
206 we won't be able to declare any other variables or functions of this type
207 for <TT>c1</TT>, <TT>c2</TT>, and <TT>c3</TT> to play with.
208 (Finally, it's also possible to declare just the tag name,
209 leaving both the list of members
210 and any declarations of variables for later,
212 this
214 is only needed in certain fairly rare and obscure situations.)
215 </p><p>Because a structure definition can also declare variables,
216 it's important not to forget the semicolon
217 at the end of a structure definition.
218 If you accidentally write
219 <pre>
220 struct complex
222 double real;
223 double imag;
225 </pre>
226 without a semicolon,
227 the compiler will keep looking for something later in the file
228 and try to declare it
229 as being
230 of type <TT>struct complex</TT>,
231 which will either result in a confusing error message or
232 (if the compiler succeeds)
233 a confusing misdeclaration.
234 </p><hr>
236 Read sequentially:
237 <a href="sx1.html" rev=precedes>prev</a>
238 <a href="sx1b.html" rel=precedes>next</a>
239 <a href="sx1.html" rev=subdocument>up</a>
240 <a href="top.html">top</a>
241 </p>
243 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
244 // <a href="copyright.html">Copyright</a> 1996-1999
245 // <a href="mailto:scs@eskimo.com">mail feedback</a>
246 </p>
247 </body>
248 </html>