Last Updated: January 3, 2026
If you’ve gotten comfortable with pointers, you’re already halfway there. Pointer arithmetic allows you to navigate arrays and other data structures more efficiently, acting like a map in a landscape of data.
Let’s explore how pointer arithmetic works in C++, why it’s useful, and some of the common pitfalls that can trip you up along the way.
At its core, pointer arithmetic involves performing mathematical operations on pointers. Think of a pointer as an address in memory. When you add or subtract an integer from a pointer, you're not just manipulating the raw address; you’re effectively moving through a sequence of memory locations, typically an array.
In C++, you can perform the following operations on pointers:
ptr + n moves the pointer n elements forward.ptr - n moves the pointer n elements backward.*ptr to access the value at the pointer's address.Here’s a quick example to illustrate pointer addition:
In this example, adding 2 to ptr moves it from arr[0] to arr[2].
Pointer arithmetic is critical for performance and memory efficiency. When working with large arrays or data structures, iterating through elements using pointers can be much faster than using array indexing, particularly in performance-sensitive applications like graphics or real-time systems.
Incrementing or decrementing pointers is a straightforward yet powerful aspect of pointer arithmetic. It's essential to understand how these operations affect pointer values.
When you increment a pointer, the pointer moves to the next element of the type it points to. So, if you have a pointer to an int, incrementing it actually adds sizeof(int) bytes to the pointer.
Decrementing works similarly, moving the pointer back by one element of its type.
In this example, decrementing the pointer allows us to traverse the array backward.
You might find yourself needing to calculate the difference between two pointers. This can be especially useful when working with arrays.
The expression ptr1 - ptr2 yields the number of elements between the two pointers, provided they point to the same array.
When calculating the difference:
Pointer arithmetic shines in situations where performance and memory usage are paramount. Here are a few scenarios where you might leverage it:
When you dynamically allocate arrays using new, pointer arithmetic becomes essential for traversing elements efficiently.
When creating data structures like linked lists or binary trees, pointer arithmetic helps navigate the nodes efficiently.
Pointer arithmetic can introduce subtle bugs if you’re not careful. Here are some common pitfalls to watch out for:
Always ensure your pointer arithmetic stays within the bounds of the allocated memory. Accessing memory outside the bounds leads to undefined behavior.
Be mindful of the type of pointers you’re using. If you increment a pointer of one type but treat it as another, you can get unexpected results.
When performing arithmetic with pointers of different types, ensure they are compatible. This can lead to confusing results.
Now that you understand pointer arithmetic, you are ready to explore how it relates to arrays in C++.
In the next chapter, we will look at how pointer arithmetic directly interacts with arrays, enhancing your understanding of both concepts and their practical applications.