Pointer Arithmetic

Pointer arithmetic in C++ allows you to perform mathematical operations on pointers. It is mainly used for traversing arrays and working with memory efficiently.

What is Pointer Arithmetic?

Pointer arithmetic means performing operations like increment, decrement, addition, and subtraction on pointers.

Since pointers store memory addresses, these operations move the pointer to different memory locations.

Why Use Pointer Arithmetic?

Pointer arithmetic is useful because it:

  • Helps traverse arrays efficiently
  • Provides direct memory access
  • Improves performance
  • Is widely used in low-level programming

Basic Pointer Arithmetic Operations

C++ supports the following operations on pointers:

  • Increment (++)
  • Decrement (--)
  • Addition (+)
  • Subtraction (-)
  • Difference between pointers

Incrementing a Pointer

Incrementing a pointer moves it to the next memory location of its data type.

#include <iostream>
using namespace std;

int main() {

int arr[3] = {10, 20, 30};

int *ptr = arr;

cout << *ptr << endl;

ptr++;

cout << *ptr << endl;

return 0;
}

Output

10
20

How Increment Works

If an integer takes 4 bytes:

  • First address → 1000
  • Next address → 1004
  • Next address → 1008

The pointer automatically moves according to data type size.

Decrementing a Pointer

ptr--;

This moves the pointer to the previous memory location.

Adding Value to Pointer

ptr = ptr + 2;

This moves the pointer forward by 2 elements.

Example of Addition

#include <iostream>
using namespace std;

int main() {

int arr[5] = {1, 2, 3, 4, 5};

int *ptr = arr;

ptr = ptr + 3;

cout << *ptr;

return 0;
}

Output

4

Subtracting Pointers

You can find the distance between two pointers.

#include <iostream>
using namespace std;

int main() {

int arr[5] = {10, 20, 30, 40, 50};

int *ptr1 = &arr[1];
int *ptr2 = &arr[4];

cout << ptr2 - ptr1;

return 0;
}

Output

3

Pointer Arithmetic with Arrays

Arrays and pointers are closely connected.

int arr[3] = {5, 10, 15};

cout << *(arr + 1);

Output

10

Important Rules of Pointer Arithmetic

  • Arithmetic works based on data type size
  • Only valid within array boundaries
  • Cannot multiply or divide pointers
  • Avoid accessing invalid memory locations

Advantages of Pointer Arithmetic

  • Faster array traversal
  • Efficient memory handling
  • Useful in system programming
  • Reduces indexing overhead

Risks of Incorrect Pointer Arithmetic

  • Accessing invalid memory
  • Program crashes
  • Undefined behavior
  • Security issues

Why Pointer Arithmetic is Important

Pointer arithmetic is important because it:

  • Supports efficient memory operations
  • Helps in array processing
  • Is used in data structures
  • Is essential in advanced C++ programming

Real-Life Example

Think of hotel room numbers:

  • Moving from one room to the next follows a sequence
  • Pointer arithmetic moves between memory locations similarly

Conclusion

Pointer arithmetic in C++ allows programmers to navigate memory efficiently using pointers. It is widely used in arrays, dynamic memory management, and advanced programming concepts for high-performance applications.

Home » Intermediate C++ > Pointers > Pointer Arithmetic