Pointers to elements of arrays are allowed to point to a valid element, or one past the end. If you increment a pointer in a way that goes more than one past the end, the behavior is undefined.
For your 0-sized array, p
is already pointing one past the end, so incrementing it is not allowed.
See C++17 8.7/4 regarding the +
operator (++
has the same restrictions):
f the expression
P
points to elementx[i]
of an array objectx
with n elements, the expressionsP + J
andJ + P
(whereJ
has the valuej
) point to the (possibly-hypothetical) elementx[i+j]
if 0≤i+j≤n; otherwise, the behavior is undefined.