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
5.9: Pointers vs. Multi-dimensional Arrays
</title>
10 <link href=
"sx8h.html" rev=precedes
>
11 <link href=
"sx8j.html" rel=precedes
>
12 <link href=
"sx8.html" rev=subdocument
>
15 <H2>section
5.9: Pointers vs. Multi-dimensional Arrays
</H2>
17 <p>Actually, some people
18 (and not just newcomers)
20 confused about the difference
21 between a one-dimensional array and a single pointer,
23 moving to two-dimensional arrays, arrays of pointers,
24 and pointers to pointers only makes things worse.
25 (But don't lose heart:
26 if you pay attention and keep your head screwed on straight,
27 you should be able to keep the differences clearly in mind.)
28 </p><p>The adjective ``syntactically'' in the paragraph at the bottom of
29 the page is significant:
32 </pre>an immediate reference to
<TT>b[
3][
4]
</TT>
33 would
<em>not
</em> be completely legal.
34 It wouldn't be a syntax error or anything,
35 but when the compiler tried to fetch the third pointer
36 and then the fourth integer pointed to,
37 it would go off into deep space,
38 because there
<em>isn't
</em> a third pointer
40 and it doesn't point anywhere.
41 </p><p>You might want to draw a picture of the data structures that would result
42 ``[a]ssuming that each element of
<TT>b
</TT>
43 does point to a twenty-element array,''
44 and verify that there are ``
200 <TT>int
</TT>s set aside,
45 plus ten cells for the pointers.''
46 (The picture will be similar to the one on the next page.)
47 </p><p>Actually, I'm not sure if having rows of different lengths
48 is the only important advantage of using a pointer array.
49 Another is that the size of the arrays
51 can be decided at run-time;
52 another is that the pointers make certain manipulations easier
53 (such as the sorting example we worked through in section
5.6).
55 </p><p>Do study the pictures on this page carefully,
56 and make sure you understand
57 the representations of the
<TT>name
</TT> and
<TT>aname
</TT> arrays
59 (You might want to refer back
60 to the similar discussion of
<TT>pmessage
</TT> and
<TT>amessage
</TT>
61 on page
104 in section
5.5.)
65 <a href=
"sx8h.html" rev=precedes
>prev
</a>
66 <a href=
"sx8j.html" rel=precedes
>next
</a>
67 <a href=
"sx8.html" rev=subdocument
>up
</a>
68 <a href=
"top.html">top
</a>
71 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
72 //
<a href=
"copyright.html">Copyright
</a> 1995,
1996
73 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>