12.5 POINTER ARITHMETIC

Pointer arithmetic allows us to use pointers in very interesting manner. Let a pointer point to one of the elements of an array. Now you can treat pointer like an integer and add a small constant say “n”. Please note that the result of this is a pointer pointing to nth location from the current location (where pointer is pointing). If you increment the pointer by one, it will start pointing to the next element. Pointer can also be decremented by a small constant. Its effect will be similar to addition, except the direction will be opposite. It means after p–– p will point to previous location. (p–3) will mean 3rd previous location.

Note: The pointer on its own does not know the array bounds. It is solely the responsibility ...

Get Object Oriented Programming with C++, Second Edition now with the O’Reilly learning platform.

O’Reilly members experience books, live events, courses curated by job role, and more from O’Reilly and nearly 200 top publishers.