Earlier you learned that it is legal to compare the value of one pointer to that of another. However, in order for a pointer comparison to be meaningful, the two pointers must have some relationship to each other. The most common way such a relationship is established is when both pointers point to elements of the same array. For example, given two pointers, A and B, that both point into the same array, if A is less than B then A points to an element at a smaller index than the element pointed to by B. Such comparisons are especially useful for determining boundary conditions. The following program demonstrates a pointer comparison. The program creates two pointer variables. One, called start, initially points to the beginning of an array, and the other, called end, points to the end of the array. As the user enters numbers, the array is filled sequentially from the beginning to the end. Each time a number is entered into the array, start is incremented. To determine if the array is full, the program simply compares start with end. When start is greater than end, the array has been filled. Once the array is full, the contents of the array are displayed.
As this program illustrates, because start and end both point to a common object, in this case the array num, they can be meaningfully compared. This type of pointer comparison is used frequently in professionally written C++ code.