In the standard's specification for pointer arithmetic ([expr.add]/4.2
, we have:
Otherwise, if
P
points to an array element i of an array objectx
with n elements ([dcl.array]), the expressionsP + J
andJ + P
(whereJ
has the value j) point to the (possibly-hypothetical) array element i + j ofx
if 0 ≤ i + j ≤ n and the expressionP - J
points to the (possibly-hypothetical) array element i − j ofx
if 0 ≤ i − j ≤ n.
What does "possibly-hypothetical" mean here? The passage already constrains the resulting pointer to be in range of the array. Well, including the one-past-the-end slot. Is that what it's referring to?
Pointer arithmetic provides the programmer with a single way of dealing with different types: adding and subtracting the number of elements required instead of the actual offset in bytes.
Pointer Arithmetic in C++: These are addition and subtraction operations. A pointer arithmetic in C++ may be incremented or decremented. It means that we can add or subtract integer value to and from the pointer. Similarly, a pointer arithmetic can be subtracted( or added) from another.
Yes, it's the one-past-the-end "element".
[basic.compound]/3:
[..] For purposes of pointer arithmetic ([expr.add]) and comparison ([expr.rel], [expr.eq]), a pointer past the end of the last element of an arrayx
of n elements is considered to be equivalent to a pointer to a hypothetical array element n ofx
and an object of typeT
that is not an array element is considered to belong to an array with one element of typeT
. [..]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With