|
1058 | 1058 | or from another array.
|
1059 | 1059 |
|
1060 | 1060 | \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; |
1063 | 1065 | only the first of
|
1064 | 1066 | the constant expressions that specify the bounds
|
1065 | 1067 | of the arrays may be omitted.
|
|
1088 | 1090 |
|
1089 | 1091 | \pnum
|
1090 | 1092 | \begin{example}
|
1091 |
| -\indextext{example!subscripting}% |
1092 | 1093 | \indextext{example!array}%
|
1093 | 1094 | \begin{codeblock}
|
1094 | 1095 | float fa[17], *afp[17];
|
|
1100 | 1101 | pointers to
|
1101 | 1102 | \tcode{float}
|
1102 | 1103 | numbers.
|
1103 |
| -\indextext{declarator!multidimensional array}% |
1104 |
| -For another example, |
| 1104 | +\end{example} |
1105 | 1105 |
|
| 1106 | +\pnum |
| 1107 | +\begin{example} |
1106 | 1108 | \begin{codeblock}
|
1107 |
| -static int x3d[3][5][7]; |
| 1109 | +int x3d[3][5][7]; |
1108 | 1110 | \end{codeblock}
|
1109 | 1111 |
|
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, |
1111 | 1117 | 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. |
1118 | 1118 | Any of the expressions
|
1119 | 1119 | \tcode{x3d},
|
1120 | 1120 | \tcode{x3d[i]},
|
1121 | 1121 | \tcode{x3d[i][j]},
|
1122 | 1122 | \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} |
1124 | 1171 | \begin{codeblock}
|
1125 | 1172 | extern int x[10];
|
1126 | 1173 | struct S {
|
|
1172 | 1219 | appearance, subscripting is a commutative operation.
|
1173 | 1220 | \end{note}
|
1174 | 1221 |
|
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 |
| - |
1249 | 1222 | \rSec2[dcl.fct]{Functions}%
|
1250 | 1223 | \indextext{declarator!function|(}
|
1251 | 1224 |
|
|
0 commit comments