AlgoMaster Logo

Pointer Arithmetic

Last Updated: January 3, 2026

6 min read

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.

Understanding Pointer Arithmetic

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.

Basic Operations

In C++, you can perform the following operations on pointers:

  • Addition: ptr + n moves the pointer n elements forward.
  • Subtraction: ptr - n moves the pointer n elements backward.
  • Dereferencing: Using *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 and Decrementing Pointers

Incrementing or decrementing pointers is a straightforward yet powerful aspect of pointer arithmetic. It's essential to understand how these operations affect pointer values.

Incrementing a Pointer

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 a 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.

Pointer Difference and Subtraction

You might find yourself needing to calculate the difference between two pointers. This can be especially useful when working with arrays.

Calculating the Difference

The expression ptr1 - ptr2 yields the number of elements between the two pointers, provided they point to the same array.

Important Considerations

When calculating the difference:

  • Ensure both pointers point to elements within the same array.
  • Using pointers from different arrays or memory blocks leads to undefined behavior.

Real-World Applications of Pointer Arithmetic

Pointer arithmetic shines in situations where performance and memory usage are paramount. Here are a few scenarios where you might leverage it:

1. Iterating Through Dynamic Arrays

When you dynamically allocate arrays using new, pointer arithmetic becomes essential for traversing elements efficiently.

2. Implementing Data Structures

When creating data structures like linked lists or binary trees, pointer arithmetic helps navigate the nodes efficiently.

Common Pitfalls to Avoid

Pointer arithmetic can introduce subtle bugs if you’re not careful. Here are some common pitfalls to watch out for:

1. Out-of-Bounds Access

Always ensure your pointer arithmetic stays within the bounds of the allocated memory. Accessing memory outside the bounds leads to undefined behavior.

2. Incorrect Pointer Types

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.

3. Mixing Pointer Types

When performing arithmetic with pointers of different types, ensure they are compatible. This can lead to confusing results.

What's Next

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.