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. -->
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
>
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
23 There are a few important points towards the end of the section,
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:
33 (but not access, or ``dereference'')
34 a pointer to the imaginary element
35 which sits one past the end of an array.
37 a common idiom for accessing an array using a pointer looks
43 for (ip =
&a[
0]; ip
< &a[
10]; ip++)
47 int *endp =
&a[
10];
51 for (ip = a; ip
< endp; ip++)
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>&a[
10]
</TT>,
56 and use it in expressions like
57 <TT>ip
< &a[
10]
</TT>
59 <TT>endp =
&a[
10]
</TT>.
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).
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>
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>