@@ -55,7 +55,7 @@ assert(*p == 6);
55
55
))
56
56
57
57
$(P To set a pointer to point at an existing object, use the
58
- `&` *address of* operator:
58
+ `&` *address of* operator:)
59
59
60
60
$(SPEC_RUNNABLE_EXAMPLE_RUN
61
61
---------
@@ -224,6 +224,28 @@ assert(b[1] == 2);
224
224
225
225
$(P See also $(GLINK2 expression, IndexExpression).)
226
226
227
+ $(H3 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
228
+
229
+ $(P A pointer can also be indexed, but no bounds checks are done.
230
+ Unlike arrays, a pointer value can also be used in certain
231
+ arithmetic expressions to produce another pointer:)
232
+
233
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
234
+ ---
235
+ int[] a = [1,2,3];
236
+ int* p = a.ptr;
237
+
238
+ *(p + 2) = 4; // same as `p[2] = 4`
239
+ assert(a[2] == 4);
240
+
241
+ assert(p == &a[0]);
242
+ p++;
243
+ assert(*p == 2);
244
+ ---
245
+ )
246
+
247
+ $(P See $(DDSUBLINK spec/expression, pointer_arithmetic, *AddExpression*) for details.)
248
+
227
249
$(H2 $(LNAME2 slicing, Slicing))
228
250
229
251
$(P $(I Slicing) an array means to specify a subarray of it.
@@ -477,30 +499,6 @@ a[] -= (b[] + 4) * c[];
477
499
the target computer.
478
500
)
479
501
480
- $(H2 $(LNAME2 pointer-arithmetic, Pointer Arithmetic))
481
-
482
- $(SPEC_RUNNABLE_EXAMPLE_FAIL
483
- ---------
484
- void dibb(int* array)
485
- {
486
- array[2]; // means same thing as *(array + 2)
487
- *(array + 2); // get 3rd element
488
- }
489
-
490
- void diss(int[] array)
491
- {
492
- array[2]; // ok
493
- *(array + 2); // error, array is not a pointer
494
- }
495
-
496
- void ditt(int[3] array)
497
- {
498
- array[2]; // ok
499
- *(array + 2); // error, array is not a pointer
500
- }
501
- ---------
502
- )
503
-
504
502
$(H2 $(LNAME2 rectangular-arrays, Rectangular Arrays))
505
503
506
504
$(P Experienced FORTRAN numerics programmers know that multidimensional
0 commit comments