* some tutorial and the ansi C book
[mascara-docs.git] / C / the.ansi.c.programming.language / notes.accompany.ansi.c / sx9e.html
blobf3e73a31892331ceeecff936eb8e5b91da80cb18
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.4: Pointers to Structures</title>
10 <link href="sx9d.html" rev=precedes>
11 <link href="sx9f.html" rel=precedes>
12 <link href="sx9.html" rev=subdocument>
13 </head>
14 <body>
15 <H2>section 6.4: Pointers to Structures</H2>
17 <p>The bulk of this section illustrates how to rewrite the
18 <TT>binsearch</TT> function
19 (which we've already been glossing over)
20 in terms of pointers instead of arrays
21 (an exercise which we've
22 been downplaying).
23 There are a few important points towards the end of the section,
24 however.
25 </p><p>page 138
26 </p><p>When we began talking about pointers and arrays,
27 we said that it was important never to access outside of the
28 defined and allocated bounds of an array,
29 either with an out-of-range index
30 or an out-of-bounds pointer.
31 There is one exception:
32 you may compute
33 (but not access, or ``dereference'')
34 a pointer to the imaginary element
35 which sits one past the end of an array.
36 Therefore,
37 a common idiom for accessing an array using a pointer looks
38 like
39 <pre> int a[10];
40 int *ip;
41 <br>
42 <br>
43 for (ip = &amp;a[0]; ip &lt; &amp;a[10]; ip++)
44 ...
45 </pre>or
46 <pre> int a[10];
47 int *endp = &amp;a[10];
48 int *ip;
49 <br>
50 <br>
51 for (ip = a; ip &lt; endp; ip++)
52 ...
53 </pre>The element <TT>a[10]</TT> does not exist
54 (the allocated elements run from <TT>[0]</TT> to <TT>[9]</TT>),
55 but we may compute the pointer <TT>&amp;a[10]</TT>,
56 and use it in expressions like
57 <TT>ip &lt; &amp;a[10]</TT>
58 and
59 <TT>endp = &amp;a[10]</TT>.
60 </p><p>Deep sentence:
61 <blockquote>Don't assume, however,
62 that the size of a structure is the sum of the sizes of its members.
63 </blockquote>If this isn't the sort of thing you'd be likely to assume,
64 you don't have to remember the reason,
65 which is mildly esoteric
66 (having to do with memory alignment requirements).
67 </p><hr>
68 <p>
69 Read sequentially:
70 <a href="sx9d.html" rev=precedes>prev</a>
71 <a href="sx9f.html" rel=precedes>next</a>
72 <a href="sx9.html" rev=subdocument>up</a>
73 <a href="top.html">top</a>
74 </p>
75 <p>
76 This page by <a href="http://www.eskimo.com/~scs/">Steve Summit</a>
77 // <a href="copyright.html">Copyright</a> 1995, 1996
78 // <a href="mailto:scs@eskimo.com">mail feedback</a>
79 </p>
80 </body>
81 </html>