Comparing Pointers

The special nature of a pointer variable—the fact that it contains an address— precludes most operations that are legal for other variables. There's no such thing as a fractional memory address, for example. So it wouldn't make sense to divide a pointer, or add a floating-point number to it. The most common pointer operations are assignment, incrementing, and decrementing, as described earlier. You can also compare one pointer to another.

If a program allocates memory for a stack, for instance, you might create two pointers that point to different parts of the stack. One pointer can show where the stack begins and the other where it ends. To see how much of the stack is in use, you can subtract the pointers. (A “stack” is a memory area used for temporary storage.)

Summary: You can compare pointer variables with relational operators or by subtraction.

Pointer comparisons can be done with relational operators (such as < ) or by subtracting one pointer from another. Of course, pointer comparisons are meaningful only for pointers that point to the same data object or related objects of the same type.