* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx9a.html
blob36a6fbf67111f95aeab0b3547eb3bc7bf62e471e
1 <!DOCTYPE HTML PUBLIC "-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995, 1996 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>section 6.1: Basics of Structures</title>
10 <link href="sx9.html" rev=precedes>
11 <link href="sx9b.html" rel=precedes>
12 <link href="sx9.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 6.1: Basics of Structures</H2>
17 <p>Don't get too excited about the prospect of doing graphics
18 in C--there's
19 no one standard or portable way of doing it,
20 so the points and rectangles we're going to be discussing
21 must remain abstract for now
22 (we won't be able to plot them out).
23 </p><p>page 128
24 </p><p>To summarize the syntax of structure declarations:
25 A structure declaration has about four parts,
26 most of them optional:
27 the keyword <TT>struct</TT>,
28 a <dfn>structure tag</dfn> (optional),
29 a brace-enclosed list of declarations for the
30 <dfn>members</dfn>
31 (also called ``fields'' or ``components'')
32 of the structure (optional),
33 and a list of variables of the new structure type (optional).
34 The arrangement looks like this:
35 <pre> struct <I>tag</I> {
36 <I>member declarations</I>
37 } <I>declared variables</I> ;
38 </pre>Normally, a structure declaration defines either a tag and the members,
39 or some variables based on an existing tag,
40 or sometimes all three at once.
41 That is,
42 we might first declare a structure:
43 <pre> struct point { /* 1 */
44 int x;
45 int y;
47 </pre>and then some variables of that type:
48 <pre> struct point here, there; /* 2 */
49 </pre>Or, we could combine the two:
50 <pre> struct point { /* 3 */
51 int x;
52 int y;
53 } here, there;
54 </pre>The list of members
55 (if present)
56 describes what the new structure ``looks like inside.''
57 The list of variables
58 (if present)
60 (obviously)
61 the list of variables of this new type
62 which we're defining
63 and which the rest of the program will use.
64 The tag
65 (if present)
66 is just an arbitrary name for the structure type itself
67 (not for any variable we're defining).
68 The tag is used to associate a structure definition
69 (as in fragment 1)
70 with a later declaration of variables of that same type
71 (as in fragment 2).
72 </p><p>One thing to beware of:
73 when you declare the members of a structure without defining any variables,
74 always remember
75 the trailing semicolon,
76 as shown
77 in fragment 1
78 above.
79 (If you forget it,
80 the compiler will wait until the next thing it finds in your source file,
81 and try to define <em>that</em>
82 as a variable or function of the structure type.)
83 </p><hr>
84 <p>
85 Read sequentially:
86 <a href="sx9.html" rev=precedes>prev</a>
87 <a href="sx9b.html" rel=precedes>next</a>
88 <a href="sx9.html" rev=subdocument>up</a>
89 <a href="top.html">top</a>
90 </p>
91 <p>
92 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
93 // <a href="copyright.html">Copyright</a> 1995, 1996
94 // <a href="mailto:scs@eskimo.com">mail feedback</a>
95 </p>
96 </body>
97 </html>