File tree Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Expand file tree Collapse file tree 2 files changed +24
-3
lines changed Original file line number Diff line number Diff line change @@ -235,11 +235,12 @@ $(SPEC_RUNNABLE_EXAMPLE_RUN
235
235
int[] a = [1,2,3];
236
236
int* p = a.ptr;
237
237
238
- *(p + 2) = 4; // same as `p [2] = 4`
238
+ p [2] = 4;
239
239
assert(a[2] == 4);
240
+ writeln(p[3]); // undefined behaviour
240
241
241
242
assert(p == &a[0]);
242
- p++;
243
+ p++; // point to a[1]
243
244
assert(*p == 2);
244
245
---
245
246
)
Original file line number Diff line number Diff line change @@ -813,6 +813,20 @@ $(GNAME AddExpression):
813
813
the size of the type pointed to by the first operand.
814
814
)
815
815
816
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
817
+ ---
818
+ int[] a = [1,2,3];
819
+ int* p = a.ptr;
820
+ assert(*p == 1);
821
+
822
+ *(p + 2) = 4; // same as `p[2] = 4`
823
+ assert(a[2] == 4);
824
+ ---
825
+ )
826
+
827
+ $(P $(GLINK IndexExpression) can also be used with a pointer and has
828
+ the same behaviour as adding an integer.)
829
+
816
830
$(P If the second operand is a pointer, and the first is an integral type,
817
831
and the operator is $(D +),
818
832
the operands are reversed and the pointer arithmetic just described
@@ -831,7 +845,13 @@ $(GNAME AddExpression):
831
845
The type of the result is $(D ptrdiff_t).
832
846
)
833
847
834
- $(P $(GLINK IndexExpression) can also be used with a pointer.)
848
+ $(SPEC_RUNNABLE_EXAMPLE_RUN
849
+ ---
850
+ int[] a = [1,2,3];
851
+ ptrdiff_t d = &a[2] - a.ptr;
852
+ assert(d == 2);
853
+ ---
854
+ )
835
855
836
856
$(H2 $(LNAME2 cat_expressions, Cat Expressions))
837
857
You can’t perform that action at this time.
0 commit comments