|
|
|
|
|
by int_19h
3462 days ago
|
|
No, he is correct. The C and C++ standards do allow pointers past the last element of an array to be produced via a pointer to an element of said array. They also have a provision where a pointer to a nonarray value is treated as if it were a 1-element array (so you can do "int x; int* p = *x + 1"). But in this case, the value is obviously an array object, and it's not an element to another array; hence, it is not legal to do &arr + 1 ("... otherwise, the behavior is undefined"). This is 6.3.6 "Additive operators" in ISO C90 standard, for anyone curious. |
|
> They also have a provision where a pointer to a nonarray value is treated as if it were a 1-element array
That is not what it says. Let me quote the exact words (emphasis mine):
For the purposes of these operators, a pointer to an object that is not an element of an array behaves the same as a pointer to the first element of an array of length one with the type of the object as its element type.
You're right that what we have is obviously (a pointer to) an array object, not an element of another array. This is precisely the case where this special provision kicks in, and thus it is legal to do &arr + 1.