@@ -124,35 +124,66 @@ int[]* e; // pointer to dynamic array of ints
124
124
$(H2 $(LNAME2 usage, Array Usage))
125
125
126
126
$(P There are two broad kinds of operations to do on an array -
127
- affecting
128
- the handle to the array,
129
- and affecting the contents of the array.
127
+ those affecting the handle to the array,
128
+ and those affecting the contents of the array.
130
129
)
131
130
132
131
$(P The handle to an array is specified by naming the array, as
133
132
in p, s or a:
134
133
)
135
134
136
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
135
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
137
136
---------
138
137
int* p;
139
138
int[3] s;
140
139
int[] a;
141
140
142
- int[] b;
143
-
144
141
p = s.ptr; // p points to the first element of the array s.
145
142
p = a.ptr; // p points to the first element of the array a.
146
143
147
- a = p; // error, since the length of the array pointed
148
- // to by p is unknown
144
+ // error, since the length of the array pointed to by p is unknown
145
+ //s = p;
146
+ //s = a; // error, as a's length is not known at compile-time
147
+
148
+ //a = p; // error, length unknown
149
149
a = s; // a is initialized to point to the s array
150
+ assert(a.ptr == s.ptr);
151
+
152
+ int[] b;
150
153
a = b; // a points to the same array as b does
154
+ assert(a.ptr == b.ptr);
151
155
---------
152
156
)
157
+ $(P Each of the three error lines above can be made to copy elements
158
+ using $(RELATIVE_LINK2 slicing, slicing), so that the number of elements
159
+ to copy is then known.)
160
+
161
+ $(H2 $(LNAME2 literals, Array Literals))
162
+
163
+ ---
164
+ auto a1 = [1,2,3]; // type is int[], with elements 1, 2 and 3
165
+ a1 = []; // a1 is now empty
166
+ auto a2 = [1u,2,3]; // type is uint[], with elements 1u, 2u, and 3u
167
+ int[2] = [1, 2];
168
+ ---
169
+
170
+ $(P See $(DDSUBLINK expression, array_literals, Array Literals).)
153
171
154
172
$(H2 $(LNAME2 indexing, Indexing))
155
173
174
+ $(P Indexing allows access to an element of an array:)
175
+
176
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
177
+ ---------
178
+ auto a = [1,2,3];
179
+ assert(a[0] == 1);
180
+ assert(a[2] == 3);
181
+ a[2] = 4;
182
+ assert(a[2] == 4);
183
+ assert(a == [1,2,4]);
184
+ ---------
185
+ )
186
+
156
187
$(P See also $(GLINK2 expression, IndexExpression).)
157
188
158
189
$(H2 $(LNAME2 slicing, Slicing))
@@ -181,7 +212,7 @@ foo(b[1]); // equivalent to foo(3)
181
212
---------
182
213
)
183
214
184
- $(P The [] is shorthand for a slice of the entire array.
215
+ $(P $(I Identifier) [] is shorthand for a slice of the entire array.
185
216
For example, the assignments to b:
186
217
)
187
218
@@ -255,8 +286,10 @@ $(H3 $(LNAME2 overlapping-copying, Overlapping Copying))
255
286
256
287
$(P Overlapping copies are an error:)
257
288
258
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
289
+ $(SPEC_RUNNABLE_EXAMPLE_COMPILE
259
290
---------
291
+ int[3] s;
292
+
260
293
s[0..2] = s[1..3]; // error, overlapping copy
261
294
s[1..3] = s[0..2]; // error, overlapping copy
262
295
---------
@@ -410,9 +443,6 @@ $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
410
443
411
444
$(SPEC_RUNNABLE_EXAMPLE_FAIL
412
445
---------
413
- int[3] abc; // static array of 3 ints
414
- int[] def = [ 1, 2, 3 ]; // dynamic array of 3 ints
415
-
416
446
void dibb(int* array)
417
447
{
418
448
array[2]; // means same thing as *(array + 2)
@@ -694,7 +724,7 @@ $(H2 $(LNAME2 bounds, Array Bounds Checking))
694
724
695
725
$(P It is an error to index an array with an index that is less than
696
726
0 or greater than or equal to the array length. If an index is
697
- out of bounds, a RangeError exception is
727
+ out of bounds, a ` RangeError` exception is
698
728
raised if detected at runtime, and an error if detected at compile
699
729
time. A program may not rely on array bounds checking happening, for
700
730
example, the following program is incorrect:
0 commit comments