Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

What does "possibly-hypothetical" mean in the pointer arithmetic rules?

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 object x with n elements ([dcl.array]), the expressions P + J and J + P (where J has the value j) point to the (possibly-hypothetical) array element i + j of x if 0 ≤ i + j ≤ n and the expression P - J points to the (possibly-hypothetical) array element i − j of x 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?

like image 226
Lightness Races in Orbit Avatar asked Jan 04 '20 15:01

Lightness Races in Orbit


People also ask

What does pointer arithmetic do?

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.

What is pointer arithmetic explain with example in C++?

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.


1 Answers

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 array x of n elements is considered to be equivalent to a pointer to a hypothetical array element n of x and an object of type T that is not an array element is considered to belong to an array with one element of type T. [..]

like image 157
Lightness Races in Orbit Avatar answered Sep 29 '22 08:09

Lightness Races in Orbit