1 | <!DOCTYPE html PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN" "http://www.w3.org/TR/html4/loose.dtd">
|
---|
2 | <html>
|
---|
3 | <!-- Copyright (C) 2022 Richard Stallman and Free Software Foundation, Inc.
|
---|
4 |
|
---|
5 | (The work of Trevis Rothwell and Nelson Beebe has been assigned or
|
---|
6 | licensed to the FSF.)
|
---|
7 |
|
---|
8 | Permission is granted to copy, distribute and/or modify this document
|
---|
9 | under the terms of the GNU Free Documentation License, Version 1.3 or
|
---|
10 | any later version published by the Free Software Foundation; with the
|
---|
11 | Invariant Sections being "GNU General Public License," with the
|
---|
12 | Front-Cover Texts being "A GNU Manual," and with the Back-Cover
|
---|
13 | Texts as in (a) below. A copy of the license is included in the
|
---|
14 | section entitled "GNU Free Documentation License."
|
---|
15 |
|
---|
16 | (a) The FSF's Back-Cover Text is: "You have the freedom to copy and
|
---|
17 | modify this GNU manual. Buying copies from the FSF supports it in
|
---|
18 | developing GNU and promoting software freedom." -->
|
---|
19 | <!-- Created by GNU Texinfo 6.7, http://www.gnu.org/software/texinfo/ -->
|
---|
20 | <head>
|
---|
21 | <meta http-equiv="Content-Type" content="text/html; charset=utf-8">
|
---|
22 | <title>Multidimensional Arrays (GNU C Language Manual)</title>
|
---|
23 |
|
---|
24 | <meta name="description" content="Multidimensional Arrays (GNU C Language Manual)">
|
---|
25 | <meta name="keywords" content="Multidimensional Arrays (GNU C Language Manual)">
|
---|
26 | <meta name="resource-type" content="document">
|
---|
27 | <meta name="distribution" content="global">
|
---|
28 | <meta name="Generator" content="makeinfo">
|
---|
29 | <link href="index.html" rel="start" title="Top">
|
---|
30 | <link href="Symbol-Index.html" rel="index" title="Symbol Index">
|
---|
31 | <link href="index.html#SEC_Contents" rel="contents" title="Table of Contents">
|
---|
32 | <link href="Arrays.html" rel="up" title="Arrays">
|
---|
33 | <link href="Constructing-Array-Values.html" rel="next" title="Constructing Array Values">
|
---|
34 | <link href="Limitations-of-C-Arrays.html" rel="prev" title="Limitations of C Arrays">
|
---|
35 | <style type="text/css">
|
---|
36 | <!--
|
---|
37 | a.summary-letter {text-decoration: none}
|
---|
38 | blockquote.indentedblock {margin-right: 0em}
|
---|
39 | div.display {margin-left: 3.2em}
|
---|
40 | div.example {margin-left: 3.2em}
|
---|
41 | div.lisp {margin-left: 3.2em}
|
---|
42 | kbd {font-style: oblique}
|
---|
43 | pre.display {font-family: inherit}
|
---|
44 | pre.format {font-family: inherit}
|
---|
45 | pre.menu-comment {font-family: serif}
|
---|
46 | pre.menu-preformatted {font-family: serif}
|
---|
47 | span.nolinebreak {white-space: nowrap}
|
---|
48 | span.roman {font-family: initial; font-weight: normal}
|
---|
49 | span.sansserif {font-family: sans-serif; font-weight: normal}
|
---|
50 | ul.no-bullet {list-style: none}
|
---|
51 | -->
|
---|
52 | </style>
|
---|
53 |
|
---|
54 |
|
---|
55 | </head>
|
---|
56 |
|
---|
57 | <body lang="en">
|
---|
58 | <span id="Multidimensional-Arrays"></span><div class="header">
|
---|
59 | <p>
|
---|
60 | Next: <a href="Constructing-Array-Values.html" accesskey="n" rel="next">Constructing Array Values</a>, Previous: <a href="Limitations-of-C-Arrays.html" accesskey="p" rel="prev">Limitations of C Arrays</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
61 | </div>
|
---|
62 | <hr>
|
---|
63 | <span id="Multidimensional-Arrays-1"></span><h3 class="section">16.7 Multidimensional Arrays</h3>
|
---|
64 | <span id="index-multidimensional-arrays"></span>
|
---|
65 | <span id="index-array_002c-multidimensional"></span>
|
---|
66 |
|
---|
67 | <p>Strictly speaking, all arrays in C are unidimensional. However, you
|
---|
68 | can create an array of arrays, which is more or less equivalent to a
|
---|
69 | multidimensional array. For example,
|
---|
70 | </p>
|
---|
71 | <div class="example">
|
---|
72 | <pre class="example">struct chesspiece *board[8][8];
|
---|
73 | </pre></div>
|
---|
74 |
|
---|
75 | <p>declares an array of 8 arrays of 8 pointers to <code>struct
|
---|
76 | chesspiece</code>. This data type could represent the state of a chess
|
---|
77 | game. To access one square’s contents requires two array index
|
---|
78 | operations, one for each dimension. For instance, you can write
|
---|
79 | <code>board[row][column]</code>, assuming <code>row</code> and <code>column</code>
|
---|
80 | are variables with integer values in the proper range.
|
---|
81 | </p>
|
---|
82 | <p>How does C understand <code>board[row][column]</code>? First of all,
|
---|
83 | <code>board</code> is converted automatically to a pointer to the zeroth
|
---|
84 | element (at index zero) of <code>board</code>. Adding <code>row</code> to that
|
---|
85 | makes it point to the desired element. Thus, <code>board[row]</code>’s
|
---|
86 | value is an element of <code>board</code>—an array of 8 pointers.
|
---|
87 | </p>
|
---|
88 | <p>However, as an expression with array type, it is converted
|
---|
89 | automatically to a pointer to the array’s zeroth element. The second
|
---|
90 | array index operation, <code>[column]</code>, accesses the chosen element
|
---|
91 | from that array.
|
---|
92 | </p>
|
---|
93 | <p>As this shows, pointer-to-array types are meaningful in C.
|
---|
94 | You can declare a variable that points to a row in a chess board
|
---|
95 | like this:
|
---|
96 | </p>
|
---|
97 | <div class="example">
|
---|
98 | <pre class="example">struct chesspiece *(*rowptr)[8];
|
---|
99 | </pre></div>
|
---|
100 |
|
---|
101 | <p>This points to an array of 8 pointers to <code>struct chesspiece</code>.
|
---|
102 | You can assign to it as follows:
|
---|
103 | </p>
|
---|
104 | <div class="example">
|
---|
105 | <pre class="example">rowptr = &board[5];
|
---|
106 | </pre></div>
|
---|
107 |
|
---|
108 | <p>The dimensions don’t have to be equal in length. Here we declare
|
---|
109 | <code>statepop</code> as an array to hold the population of each state in
|
---|
110 | the United States for each year since 1900:
|
---|
111 | </p>
|
---|
112 | <div class="example">
|
---|
113 | <pre class="example">#define NSTATES 50
|
---|
114 | {
|
---|
115 | int nyears = current_year - 1900 + 1;
|
---|
116 | int statepop[NSTATES][nyears];
|
---|
117 | <span class="roman">…</span>
|
---|
118 | }
|
---|
119 | </pre></div>
|
---|
120 |
|
---|
121 | <p>The variable <code>statepop</code> is an array of <code>NSTATES</code> subarrays,
|
---|
122 | each indexed by the year (counting from 1900). Thus, to get the
|
---|
123 | element for a particular state and year, we must subscript it first
|
---|
124 | by the number that indicates the state, and second by the index for
|
---|
125 | the year:
|
---|
126 | </p>
|
---|
127 | <div class="example">
|
---|
128 | <pre class="example">statepop[state][year - 1900]
|
---|
129 | </pre></div>
|
---|
130 |
|
---|
131 | <span id="index-array_002c-layout-in-memory"></span>
|
---|
132 | <p>The subarrays within the multidimensional array are allocated
|
---|
133 | consecutively in memory, and within each subarray, its elements are
|
---|
134 | allocated consecutively in memory. The most efficient way to process
|
---|
135 | all the elements in the array is to scan the last subscript in the
|
---|
136 | innermost loop. This means consecutive accesses go to consecutive
|
---|
137 | memory locations, which optimizes use of the processor’s memory cache.
|
---|
138 | For example:
|
---|
139 | </p>
|
---|
140 | <div class="example">
|
---|
141 | <pre class="example">int total = 0;
|
---|
142 | float average;
|
---|
143 |
|
---|
144 | for (int state = 0; state < NSTATES, ++state)
|
---|
145 | {
|
---|
146 | for (int year = 0; year < nyears; ++year)
|
---|
147 | {
|
---|
148 | total += statepop[state][year];
|
---|
149 | }
|
---|
150 | }
|
---|
151 |
|
---|
152 | average = total / nyears;
|
---|
153 | </pre></div>
|
---|
154 |
|
---|
155 | <p>C’s layout for multidimensional arrays is different from Fortran’s
|
---|
156 | layout. In Fortran, a multidimensional array is not an array of
|
---|
157 | arrays; rather, multidimensional arrays are a primitive feature, and
|
---|
158 | it is the first index that varies most rapidly between consecutive
|
---|
159 | memory locations. Thus, the memory layout of a 50x114 array in C
|
---|
160 | matches that of a 114x50 array in Fortran.
|
---|
161 | </p>
|
---|
162 | <hr>
|
---|
163 | <div class="header">
|
---|
164 | <p>
|
---|
165 | Next: <a href="Constructing-Array-Values.html" accesskey="n" rel="next">Constructing Array Values</a>, Previous: <a href="Limitations-of-C-Arrays.html" accesskey="p" rel="prev">Limitations of C Arrays</a>, Up: <a href="Arrays.html" accesskey="u" rel="up">Arrays</a> [<a href="index.html#SEC_Contents" title="Table of contents" rel="contents">Contents</a>][<a href="Symbol-Index.html" title="Index" rel="index">Index</a>]</p>
|
---|
166 | </div>
|
---|
167 |
|
---|
168 |
|
---|
169 |
|
---|
170 | </body>
|
---|
171 | </html>
|
---|