Skip to content

Commit ab38267

Browse files
committed
Add *Pointer Arithmetic* subheading for *Indexing*
This replaces the old example, actually compiles and is simpler. Also fix missing closing ).
1 parent 7e666a8 commit ab38267

File tree

1 file changed

+23
-25
lines changed

1 file changed

+23
-25
lines changed

spec/arrays.dd

Lines changed: 23 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ assert(*p == 6);
5555
))
5656

5757
$(P To set a pointer to point at an existing object, use the
58-
`&` *address of* operator:
58+
`&` *address of* operator:)
5959

6060
$(SPEC_RUNNABLE_EXAMPLE_RUN
6161
---------
@@ -224,6 +224,28 @@ assert(b[1] == 2);
224224

225225
$(P See also $(GLINK2 expression, IndexExpression).)
226226

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+
227249
$(H2 $(LNAME2 slicing, Slicing))
228250

229251
$(P $(I Slicing) an array means to specify a subarray of it.
@@ -477,30 +499,6 @@ a[] -= (b[] + 4) * c[];
477499
the target computer.
478500
)
479501

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-
504502
$(H2 $(LNAME2 rectangular-arrays, Rectangular Arrays))
505503

506504
$(P Experienced FORTRAN numerics programmers know that multidimensional

0 commit comments

Comments
 (0)