Skip to content

Commit 9b13e37

Browse files
committed
[dcl.array] Consolidate redundant, repetitive, and somewhat incorrect
examples and notes on multidimensional arrays. Fixes #1645
1 parent d0a8ea2 commit 9b13e37

File tree

1 file changed

+61
-88
lines changed

1 file changed

+61
-88
lines changed

source/declarators.tex

Lines changed: 61 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -1058,8 +1058,10 @@
10581058
or from another array.
10591059

10601060
\pnum
1061-
When several ``array of'' specifications are adjacent, a multidimensional
1062-
array type is created;
1061+
\indextext{declarator!multidimensional array}%
1062+
When several ``array of'' specifications are adjacent, a
1063+
multidimensional array
1064+
type is created;
10631065
only the first of
10641066
the constant expressions that specify the bounds
10651067
of the arrays may be omitted.
@@ -1088,7 +1090,6 @@
10881090

10891091
\pnum
10901092
\begin{example}
1091-
\indextext{example!subscripting}%
10921093
\indextext{example!array}%
10931094
\begin{codeblock}
10941095
float fa[17], *afp[17];
@@ -1100,27 +1101,73 @@
11001101
pointers to
11011102
\tcode{float}
11021103
numbers.
1103-
\indextext{declarator!multidimensional array}%
1104-
For another example,
1104+
\end{example}
11051105

1106+
\pnum
1107+
\begin{example}
11061108
\begin{codeblock}
1107-
static int x3d[3][5][7];
1109+
int x3d[3][5][7];
11081110
\end{codeblock}
11091111

1110-
declares a static three-dimensional array of integers,
1112+
declares an array of three elements,
1113+
each of which is an array of five elements,
1114+
each of which is an array of seven integers.
1115+
The overall array can be viewed as a
1116+
three-dimensional array of integers,
11111117
with rank $3 \times 5 \times 7$.
1112-
In complete detail,
1113-
\tcode{x3d}
1114-
is an array of three items;
1115-
each item is an array of five arrays;
1116-
each of the latter arrays is an array of seven
1117-
integers.
11181118
Any of the expressions
11191119
\tcode{x3d},
11201120
\tcode{x3d[i]},
11211121
\tcode{x3d[i][j]},
11221122
\tcode{x3d[i][j][k]}
1123-
can reasonably appear in an expression. Finally,
1123+
can reasonably appear in an expression.
1124+
\indextext{example!subscripting}%
1125+
The expression
1126+
\tcode{x3d[i]}
1127+
is equivalent to
1128+
\tcode{*(x3d + i)};
1129+
in that expression,
1130+
\tcode{x3d}
1131+
is subject to the array-to-pointer conversion\iref{conv.array}
1132+
and is first converted to
1133+
a pointer to a 2-dimensional
1134+
array with rank
1135+
$5 \times 7$
1136+
that points to the first element of \tcode{x3d}.
1137+
Then \tcode{i} is added,
1138+
which on typical implementations involves multiplying
1139+
\tcode{i} by the
1140+
length of the object to which the pointer points,
1141+
which is \tcode{sizeof(int)}$ \times 5 \times 7$.
1142+
The result of the addition and indirection is
1143+
an lvalue denoting
1144+
the \tcode{i}\textsuperscript{th} array element of
1145+
\tcode{x3d}
1146+
(an array of five arrays of seven integers).
1147+
If there is another subscript,
1148+
the same argument applies again, so
1149+
\tcode{x3d[i][j]} is
1150+
an lvalue denoting
1151+
the \tcode{j}\textsuperscript{th} array element of
1152+
the \tcode{i}\textsuperscript{th} array element of
1153+
\tcode{x3d}
1154+
(an array of seven integers), and
1155+
\tcode{x3d[i][j][k]} is
1156+
an lvalue denoting
1157+
the \tcode{k}\textsuperscript{th} array element of
1158+
the \tcode{j}\textsuperscript{th} array element of
1159+
the \tcode{i}\textsuperscript{th} array element of
1160+
\tcode{x3d}
1161+
(an integer).
1162+
\end{example}
1163+
\begin{note}
1164+
The first subscript in the declaration helps determine
1165+
the amount of storage consumed by an array
1166+
but plays no other part in subscript calculations.
1167+
\end{note}
1168+
1169+
\pnum
1170+
\begin{example}
11241171
\begin{codeblock}
11251172
extern int x[10];
11261173
struct S {
@@ -1172,80 +1219,6 @@
11721219
appearance, subscripting is a commutative operation.
11731220
\end{note}
11741221

1175-
\pnum
1176-
\begin{note}
1177-
A consistent rule is followed for
1178-
\indextext{array!multidimensional}%
1179-
multidimensional arrays.
1180-
If
1181-
\tcode{E}
1182-
is an
1183-
\textit{n}-dimensional
1184-
array
1185-
of rank
1186-
$i \times j \times \dotsb \times k$,
1187-
then
1188-
\tcode{E}
1189-
appearing in an expression
1190-
that is subject to the array-to-pointer conversion\iref{conv.array}
1191-
is converted to
1192-
a pointer to an $(n-1)$-dimensional
1193-
array with rank
1194-
$j \times \dotsb \times k$.
1195-
If the
1196-
\tcode{*}
1197-
operator, either explicitly
1198-
or implicitly as a result of subscripting,
1199-
is applied to this pointer,
1200-
the result is the pointed-to $(n-1)$-dimensional array,
1201-
which itself is immediately converted into a pointer.
1202-
\begin{example}
1203-
Consider
1204-
1205-
\begin{codeblock}
1206-
int x[3][5];
1207-
\end{codeblock}
1208-
1209-
Here
1210-
\tcode{x}
1211-
is a $3 \times 5$ array of integers.
1212-
When
1213-
\tcode{x}
1214-
appears in an expression, it is converted
1215-
to a pointer to (the first of three) five-membered arrays of integers.
1216-
In the expression
1217-
\tcode{x[i]}
1218-
which is equivalent to
1219-
\tcode{*(x+i)},
1220-
\tcode{x}
1221-
is first converted to a pointer as described;
1222-
then
1223-
\tcode{x+i}
1224-
is converted to the type of
1225-
\tcode{x},
1226-
which involves multiplying
1227-
\tcode{i}
1228-
by the
1229-
length of the object to which the pointer points,
1230-
namely five integer objects.
1231-
The results are added and indirection applied to
1232-
yield an array (of five integers), which in turn is converted to
1233-
a pointer to the first of the integers.
1234-
If there is another subscript the same argument applies
1235-
again; this time the result is an integer.
1236-
\end{example}
1237-
\end{note}
1238-
1239-
\pnum
1240-
\begin{note}
1241-
It follows from all this that arrays in \Cpp are stored
1242-
row-wise (last subscript varies fastest)
1243-
\indextext{array!storage of}%
1244-
and that the first subscript in the declaration helps determine
1245-
the amount of storage consumed by an array
1246-
but plays no other part in subscript calculations.
1247-
\end{note}
1248-
12491222
\rSec2[dcl.fct]{Functions}%
12501223
\indextext{declarator!function|(}
12511224

0 commit comments

Comments
 (0)