source: public/doc/gnu-c/Multidimensional-Arrays.html@ 02598c2

Last change on this file since 02598c2 was 02598c2, checked in by Mikhail Kirillov <w96k@…>, on Oct 6, 2022 at 12:36:29 PM

Add gnu-c

  • Property mode set to 100644
File size: 7.2 KB
Line 
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
6licensed to the FSF.)
7
8Permission is granted to copy, distribute and/or modify this document
9under the terms of the GNU Free Documentation License, Version 1.3 or
10any later version published by the Free Software Foundation; with the
11Invariant Sections being "GNU General Public License," with the
12Front-Cover Texts being "A GNU Manual," and with the Back-Cover
13Texts as in (a) below. A copy of the license is included in the
14section entitled "GNU Free Documentation License."
15
16(a) The FSF's Back-Cover Text is: "You have the freedom to copy and
17modify this GNU manual. Buying copies from the FSF supports it in
18developing 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<!--
37a.summary-letter {text-decoration: none}
38blockquote.indentedblock {margin-right: 0em}
39div.display {margin-left: 3.2em}
40div.example {margin-left: 3.2em}
41div.lisp {margin-left: 3.2em}
42kbd {font-style: oblique}
43pre.display {font-family: inherit}
44pre.format {font-family: inherit}
45pre.menu-comment {font-family: serif}
46pre.menu-preformatted {font-family: serif}
47span.nolinebreak {white-space: nowrap}
48span.roman {font-family: initial; font-weight: normal}
49span.sansserif {font-family: sans-serif; font-weight: normal}
50ul.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>
60Next: <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> &nbsp; [<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
68can create an array of arrays, which is more or less equivalent to a
69multidimensional 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
76chesspiece</code>. This data type could represent the state of a chess
77game. To access one square&rsquo;s contents requires two array index
78operations, one for each dimension. For instance, you can write
79<code>board[row][column]</code>, assuming <code>row</code> and <code>column</code>
80are 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
84element (at index zero) of <code>board</code>. Adding <code>row</code> to that
85makes it point to the desired element. Thus, <code>board[row]</code>&rsquo;s
86value is an element of <code>board</code>&mdash;an array of 8 pointers.
87</p>
88<p>However, as an expression with array type, it is converted
89automatically to a pointer to the array&rsquo;s zeroth element. The second
90array index operation, <code>[column]</code>, accesses the chosen element
91from that array.
92</p>
93<p>As this shows, pointer-to-array types are meaningful in C.
94You can declare a variable that points to a row in a chess board
95like 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>.
102You can assign to it as follows:
103</p>
104<div class="example">
105<pre class="example">rowptr = &amp;board[5];
106</pre></div>
107
108<p>The dimensions don&rsquo;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
110the 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">&hellip;</span>
118}
119</pre></div>
120
121<p>The variable <code>statepop</code> is an array of <code>NSTATES</code> subarrays,
122each indexed by the year (counting from 1900). Thus, to get the
123element for a particular state and year, we must subscript it first
124by the number that indicates the state, and second by the index for
125the 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
133consecutively in memory, and within each subarray, its elements are
134allocated consecutively in memory. The most efficient way to process
135all the elements in the array is to scan the last subscript in the
136innermost loop. This means consecutive accesses go to consecutive
137memory locations, which optimizes use of the processor&rsquo;s memory cache.
138For example:
139</p>
140<div class="example">
141<pre class="example">int total = 0;
142float average;
143
144for (int state = 0; state &lt; NSTATES, ++state)
145 {
146 for (int year = 0; year &lt; nyears; ++year)
147 {
148 total += statepop[state][year];
149 }
150 }
151
152average = total / nyears;
153</pre></div>
154
155<p>C&rsquo;s layout for multidimensional arrays is different from Fortran&rsquo;s
156layout. In Fortran, a multidimensional array is not an array of
157arrays; rather, multidimensional arrays are a primitive feature, and
158it is the first index that varies most rapidly between consecutive
159memory locations. Thus, the memory layout of a 50x114 array in C
160matches that of a 114x50 array in Fortran.
161</p>
162<hr>
163<div class="header">
164<p>
165Next: <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> &nbsp; [<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>
Note: See TracBrowser for help on using the repository browser.