Supporting “one beyond the last” makes working with arrays simpler. For example, if a function is called with a pointer Start
to some element, it might want to prepare an end pointer End
and use a loop such as:
for (ElementType *p = Start; p < End; ++p)
To do this, End
must be a valid pointer.
We might consider instead setting End
to be the last element to be processed, rather than one beyond it, and using:
for (ElementType *p = Start; p <= End; ++p)
However, note that, after the final element is processed, p
will be incremented to beyond End
. Then, in order for p <= End
to be a valid expression, p
must be a valid pointer. So, we need to be able to do address arithmetic up to one beyond the last element of an array.