* X more docs for C
[mascara-docs.git] / C / the.ansi.c.programming.language / c.programming.notes.int / sx9.html
blob5cae208b8f62df19c7fefca048fdb96734b6b8b2
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>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>
13 </head>
14 <body>
15 <H1>Chapter 23: Two-Dimensional (and Multidimensional) Arrays</H1>
17 <p>C does not have true multidimensional arrays.
18 However,
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
27 more dimensions.
28 Here is a two-dimensional array:
29 <pre>
30 int a2[5][7];
31 </pre>
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:
38 <pre>
39 int i, j;
40 for(i = 0; i &lt; 5; i++)
42 for(j = 0; j &lt; 7; j++)
43 a2[i][j] = 0;
45 </pre>
46 Make sure that you remember to put each subscript in its own,
47 correct pair of brackets.
48 Neither
49 <pre>
50 int a2[5, 7]; /* XXX WRONG */
51 </pre>
52 nor
53 <pre>
54 a2[i, j] = 0; /* XXX WRONG */
55 </pre>
56 nor
57 <pre>
58 a2[j][i] = 0; /* XXX WRONG */
59 </pre>
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>
63 <hr>
64 <p>
65 Read sequentially:
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>
70 </p>
71 <p>
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>
75 </p>
76 </body>
77 </html>