Item 44. Pointer Arithmetic

Pointer arithmetic is straightforward. To understand the nature of pointer arithmetic in C++, it’s best to consider a pointer into an array:

const int MAX = 10;short points[MAX];short *curPoint = points+4;

This gives us an array and a pointer to somewhere near the middle of the array, as shown in Figure 9.

Figure 9. Effect of various arithmetic operations on the address contained in a pointer

image

If we increment or decrement the pointer curPoint, we are requesting that it point to the next or previous short in the points array. In other words, pointer arithmetic is always scaled in the size of the object that is being ...

Get C++ Common Knowledge: Essential Intermediate Programming 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.