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>4.1.2 Arrays of Arrays (``Multidimensional'' Arrays)
</title>
10 <link href=
"sx4aa.html" rev=precedes
>
11 <link href=
"sx4b.html" rel=precedes
>
12 <link href=
"sx4a.html" rev=subdocument
>
15 <H3>4.1.2 Arrays of Arrays (``Multidimensional'' Arrays)
</H3>
17 <p>[This section is optional and may be skipped.]
18 </p><p>When we said that ``Arrays are not limited to type
<TT>int
</TT>;
19 you can have arrays of... any other type,''
20 we meant that more literally than you might have guessed.
21 If you have an ``array of
<TT>int
</TT>,''
22 it means that you have an array
23 each of whose elements is of type
<TT>int
</TT>.
24 But you can have an array each of whose elements is of type
<I>x
</I>,
25 where
<I>x
</I> is any type you choose.
27 you can have an array each of whose elements is another array!
28 We can use these arrays of arrays for the same sorts of tasks
29 as we'd use multidimensional arrays in other computer languages
30 (or matrices in mathematics).
31 Naturally, we are not limited to arrays of arrays,
33 we could have an array of arrays of arrays,
36 a
3-dimensional array, etc.
37 </p><p>The declaration of an array of arrays looks like this:
41 You have to read complicated declarations like these ``inside out.''
42 What this one says is that
<TT>a2
</TT> is an array of
5 somethings,
43 and that each of the somethings is an array of
7 <TT>int
</TT>s.
45 ``
<TT>a2
</TT> is an array of
5 arrays of
7 <TT>int
</TT>s,''
47 ``
<TT>a2
</TT> is an array of array of
<TT>int
</TT>.''
48 In the declaration of
<TT>a2
</TT>,
49 the brackets closest to the identifier
<TT>a2
</TT>
50 tell you what
<TT>a2
</TT> first and foremost is.
51 That's how you know it's an array of
5 arrays of size
7,
52 not the other way around.
53 You can think of
<TT>a2
</TT>
54 as having
5 ``rows'' and
7 ``columns,''
55 although this interpretation is not mandatory.
56 (You could also treat the ``first'' or inner subscript
57 as ``x'' and the second as ``y.''
58 Unless you're doing something fancy,
59 all you have to worry about
60 is that the subscripts when you access the array
61 match those that you used when you declared it,
62 as in the examples below.)
63 </p><p>To illustrate the use of multidimensional arrays,
64 we might fill in the elements of the above array
<TT>a2
</TT>
65 using this piece of code:
68 for(i =
0; i
< 5; i = i +
1)
70 for(j =
0; j
< 7; j = j +
1)
71 a2[i][j] =
10 * i + j;
74 This pair of nested loops sets
<TT>a[
1][
2]
</TT> to
12,
75 <TT>a[
4][
1]
</TT> to
41, etc.
77 dimension of
<TT>a2
</TT> is
5,
79 subscripting index variable,
<TT>i
</TT>,
82 subscript varies from
0 to
6.
83 </p><p>We could print
<TT>a2
</TT> out
84 (in a two-dimensional way,
85 suggesting its structure)
86 with a similar pair of nested loops:
88 for(i =
0; i
< 5; i = i +
1)
90 for(j =
0; j
< 7; j = j +
1)
91 printf(
"%d\t", a2[i][j]);
95 (The character
<TT>\t
</TT> in the
<TT>printf
</TT> string
96 is the tab character.)
97 </p><p>Just to see more clearly what's going on,
98 we could make the ``row'' and ``column''
99 subscripts explicit by printing them, too:
101 for(j =
0; j
< 7; j = j +
1)
105 for(i =
0; i
< 5; i = i +
1)
108 for(j =
0; j
< 7; j = j +
1)
109 printf(
"\t%d", a2[i][j]);
113 This last fragment would print
117 1:
10 11 12 13 14 15 16
118 2:
20 21 22 23 24 25 26
119 3:
30 31 32 33 34 35 36
120 4:
40 41 42 43 44 45 46
123 there's no reason we have to loop over the ``rows'' first
124 and the ``columns'' second;
125 depending on what we wanted to do, we could interchange the two loops,
128 for(j =
0; j
< 7; j = j +
1)
130 for(i =
0; i
< 5; i = i +
1)
131 printf(
"%d\t", a2[i][j]);
135 Notice that
<TT>i
</TT> is still the first subscript
136 and it still runs from
0 to
4,
137 and
<TT>j
</TT> is still the second subscript
138 and it still runs from
0 to
6.
142 <a href=
"sx4aa.html" rev=precedes
>prev
</a>
143 <a href=
"sx4b.html" rel=precedes
>next
</a>
144 <a href=
"sx4a.html" rev=subdocument
>up
</a>
145 <a href=
"top.html">top
</a>
148 This page by
<a href=
"http://www.eskimo.com/~scs/">Steve Summit
</a>
149 //
<a href=
"copyright.html">Copyright
</a> 1995-
1997
150 //
<a href=
"mailto:scs@eskimo.com">mail feedback
</a>