C ++ programming language 3rd edition of Stroustrup says that
Subtraction of pointers is determined only when both pointers point to elements of the same array (although the language does not have a quick way in this case). When subtracting one pointer from another, the result is the number of elements in the array between the two pointers (integer). You can add an integer to the pointer or subtract the integer from the pointer; in both cases, the result is the value of the pointer. If this value does not point to an element of the same array as the source pointer or one after it, the result of using this value is undefined.
For instance:
void f () { int v1 [10]; int v2 [10]; int i1 = &v1[5] - &v1[3];
I read about unspecified behavior on Wikipedia. It says that
In C and C ++, comparing pointers with objects is strictly defined if pointers point to members of the same object or to elements of the same array.
Example:
int main(void) { int a = 0; int b = 0; return &a < &b; }
So, I'm confused. Which one is correct? Wikipedia or Straustrup book? What does the C ++ standard say about this?
Correct me if I donโt understand something.
source share