1 <!DOCTYPE HTML PUBLIC
"-//W3O//DTD W3 HTML 2.0//EN">
2 <!-- This collection of hypertext pages is Copyright 1995-7 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>Chapter
23: Two-Dimensional (and Multidimensional) Arrays
</title>
10 <link href=
"sx8.html" rev=precedes
>
11 <link href=
"sx9a.html" rel=precedes
>
12 <link href=
"top.html" rev=subdocument
>
15 <H1>Chapter
23: Two-Dimensional (and Multidimensional) Arrays
</H1>
17 <p>C does not have true multidimensional arrays.
19 because of the generality of C's type system, you can have
20 <dfn>arrays of arrays
</dfn>,
21 which are almost as good.
22 (This should not surprise you;
23 you can have arrays of anything, so why not arrays of arrays?)
24 </p><p>We'll use two-dimensional arrays as examples in this section,
25 although the techniques can
26 be extended to three or
28 Here is a two-dimensional array:
32 Formally,
<TT>a2
</TT> is an array of
5 elements, each of which is
33 an array of
7 <TT>int
</TT>s.
34 (We usually think of it as having
5 rows and
7 columns,
35 although this interpretation is not mandatory.)
36 </p><p>Just as we declared a two-dimensional array using two pairs of brackets,
37 we access its individual elements using two pairs of brackets:
40 for(i =
0; i
< 5; i++)
42 for(j =
0; j
< 7; j++)
46 Make sure that you remember to put each subscript in its own,
47 correct pair of brackets.
50 int a2[
5,
7]; /* XXX WRONG */
54 a2[i, j] =
0; /* XXX WRONG */
58 a2[j][i] =
0; /* XXX WRONG */
60 would do anything remotely like what you wanted.
61 </p><p><a href=
"sx9a.html" rel=subdocument
>23.1: Multidimensional Arrays and Functions
</a></p>
62 <p><a href=
"sx9b.html" rel=subdocument
>23.2: Dynamically Allocating Multidimensional Arrays
</a></p>
66 <a href=
"sx8.html" rev=precedes
>prev
</a>
67 <a href=
"sx9a.html" rel=precedes
>next
</a>
68 <a href=
"top.html" rev=subdocument
>up
</a>
69 <a href=
"top.html">top
</a>
72 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
73 //
<a href=
"copyright.html">Copyright
</a> 1996-
1999
74 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>