* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx9b.html
blob5cf6c00ca2c932bab391e9a12e8e9b914f98b8ca
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.2: Structures and Functions</title>
10 <link href="sx9a.html" rev=precedes>
11 <link href="sx9c.html" rel=precedes>
12 <link href="sx9.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 6.2: Structures and Functions</H2>
17 <p>In this section,
18 we'll begin playing with structures
19 more or less as if they were ordinary variables
20 such as we've been using all along
21 (which they more or less are).
22 As we'll see,
23 we can
24 declare variables of structure type,
25 declare functions which accept structures as parameters and return them,
26 declare pointers to structures,
27 take the address of a structure
28 (creating a pointer-to-structure)
29 with <TT>&amp;</TT>,
30 and assign structures.
31 </p><p>Notice that when we declare something as ``a structure type,''
32 we always have to say <em>which</em> structure type,
33 usually by using the struct tag.
34 If we've set up a ``point'' structure as above,
35 then to declare a variable of this type,
36 we say
37 <pre> struct point thepoint;
38 </pre>Both
39 <pre> struct thepoint; /* WRONG */
40 </pre>and
41 <pre> point thepoint; /* WRONG */
42 </pre>would be errors.
43 </p><p>The above list of things the language lets us do with structures
44 lets us keep them and move them around,
45 but there isn't really anything defined by the language
46 that we can <em>do</em> with structures.
47 It's up to us to define any operations on structures,
48 usually by writing functions.
49 (The <TT>addpoint</TT> function on page 130 is a good example.
50 It will make a bit more sense
51 if you think of it as adding not isolated points,
52 but rather vectors.
54 [We can't add Seattle plus Los Angeles,
55 but we could add
56 (two miles south, one mile east)
57 plus
58 (one mile east, two miles north).])
59 </p><p>page 131
60 </p><p>As an aside,
61 how safe are the <TT>min()</TT> and <TT>max()</TT>
62 macros defined at the top of page 131,
63 with respect to the criteria
64 discussed on pages 15 and 16
66 of the notes
67 on section 4.11.2 (page 90 in the text)?
68 </p><p>The precise meaning of the ``shorthand'' <TT>-&gt;</TT> operator
69 is that <TT>sp-&gt;m</TT> is,
70 by definition,
71 equivalent
73 to <TT>(*sp).m</TT>,
74 for any structure pointer <TT>sp</TT>
75 and member <TT>m</TT> of the pointed-to structure.
76 </p><hr>
77 <p>
78 Read sequentially:
79 <a href="sx9a.html" rev=precedes>prev</a>
80 <a href="sx9c.html" rel=precedes>next</a>
81 <a href="sx9.html" rev=subdocument>up</a>
82 <a href="top.html">top</a>
83 </p>
84 <p>
85 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
86 // <a href="copyright.html">Copyright</a> 1995, 1996
87 // <a href="mailto:scs@eskimo.com">mail feedback</a>
88 </p>
89 </body>
90 </html>