* better
[mascara-docs.git] / lang / C / the.ansi.c.programming.language / c.programming.notes / sx4ba.html
blob42e79d22bab465ee810d652b2f4ecf2dbc6c03ce
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. -->
5 <html>
6 <head>
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>
13 </head>
14 <body>
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.
26 In particular,
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,
32 either;
33 we could have an array of arrays of arrays,
34 which would
35 act like
36 a 3-dimensional array, etc.
37 </p><p>The declaration of an array of arrays looks like this:
38 <pre>
39 int a2[5][7];
40 </pre>
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.
44 More briefly,
45 ``<TT>a2</TT> is an array of 5 arrays of 7 <TT>int</TT>s,''
46 or,
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:
66 <pre>
67 int i, j;
68 for(i = 0; i &lt; 5; i = i + 1)
70 for(j = 0; j &lt; 7; j = j + 1)
71 a2[i][j] = 10 * i + j;
73 </pre>
74 This pair of nested loops sets <TT>a[1][2]</TT> to 12,
75 <TT>a[4][1]</TT> to 41, etc.
76 Since the first
77 dimension of <TT>a2</TT> is 5,
78 the first
79 subscripting index variable, <TT>i</TT>,
80 runs from 0 to 4.
81 Similarly, the second
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:
87 <pre>
88 for(i = 0; i &lt; 5; i = i + 1)
90 for(j = 0; j &lt; 7; j = j + 1)
91 printf("%d\t", a2[i][j]);
92 printf("\n");
94 </pre>
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:
100 <pre>
101 for(j = 0; j &lt; 7; j = j + 1)
102 printf("\t%d:", j);
103 printf("\n");
105 for(i = 0; i &lt; 5; i = i + 1)
107 printf("%d:", i);
108 for(j = 0; j &lt; 7; j = j + 1)
109 printf("\t%d", a2[i][j]);
110 printf("\n");
112 </pre>
113 This last fragment would print
114 <pre>
115 0: 1: 2: 3: 4: 5: 6:
116 0: 0 1 2 3 4 5 6
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
121 </pre>
122 </p><p>Finally,
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,
126 like this:
127 <pre>
128 for(j = 0; j &lt; 7; j = j + 1)
130 for(i = 0; i &lt; 5; i = i + 1)
131 printf("%d\t", a2[i][j]);
132 printf("\n");
134 </pre>
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.
139 </p><hr>
141 Read sequentially:
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>
146 </p>
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>
151 </p>
152 </body>
153 </html>