wleci
AboutProjectsBlogContact
Contact
Back to blog
PointersMemoryBasics

Stage 11: Pointer Arithmetic in C++

Understanding how C++ operates on memory addresses. Learn why ptr++ is more than just adding one.

February 11, 20263 min read
Share:

Pointer Arithmetic: Navigating Through Memory

Pointer arithmetic is a mechanism that allows you to perform addition and subtraction on memory addresses. It is the foundation of how arrays work in C++. The key rule to remember for your exam is that these operations are scaled by the size of the data type the pointer points to.


1. Incrementation and Decrementation (ptr++, ptr--)

When you increment a pointer by 1 (ptr++), you are not moving by one byte in memory. You move by as many bytes as the data type occupies (i.e., by sizeof(type)).

  • If an int takes 4 bytes, ptr++ increases the address by 4.
  • If a double takes 8 bytes, ptr++ increases the address by 8.
cpp
int arr[] = {10, 20, 30}; int *p = arr; // points to arr[0] p++; // p now points to arr[1] std::cout << *p; // Prints 20

2. Adding and Subtracting Integers

You can move a pointer by any number of elements by adding or subtracting an integer.

NewAddress=OldAddress+(ncdotsizeof(type))New\\_Address = Old\\_Address + (n \\cdot sizeof(type))NewA​ddress=OldA​ddress+(ncdotsizeof(type))

cpp
int *p = &arr[0]; int *p2 = p + 2; // p2 now points to arr[2]

3. Subtracting Pointers

If you have two pointers pointing to elements of the same array, you can subtract them. The result is not the number of bytes, but the number of elements between them.

cpp
int *p_start = &arr[0]; int *p_end = &arr[2]; int distance = p_end - p_start; // Result: 2

4. Relationship Between Pointers and Arrays

In C++, the arr[i] notation is just "syntactic sugar" for a pointer operation. The compiler always translates it to:

*(arr + i)

This means you can iterate through an array using just the pointer, which is often required in low-level tasks.

cpp
const int N = 3; int numbers[N] = {100, 200, 300}; int *ptr = numbers; for(int i = 0; i < N; i++) { std::cout << *(ptr + i) << " "; // Same as ptr[i] or numbers[i] }

Exam Pitfalls

  1. Multiplication and Division: You cannot multiply or divide pointers. Operations like ptr * 2 will cause a compilation error.
  2. Adding Pointers: You cannot add a pointer to another pointer (ptr1 + ptr2). It makes no logical sense.
  3. Out of Bounds: Arithmetic allows you to set a pointer outside the array. C++ won't stop you, but attempting to dereference (*) such an address will crash the program.
  4. void Pointers*: You cannot perform arithmetic on void* pointers because the compiler doesn't know the size to shift the address by.

Example: Reversing an Array with Pointers

cpp
void reverse(int *left, int *right) { while (left < right) { int temp = *left; *left = *right; *right = temp; left++; right--; } }

You might also like

PointersMemory Management

Stage 10: Pointers, Address-of, and Dereference Operators

Understanding the foundations of memory addressing in C++: how pointers and low-level operators work.

3 min read
ArraysMemory

Stage 3: Automatic One-Dimensional Arrays in C++

A guide to static one-dimensional arrays: declaration, initialization, and safe iteration.

3 min read
StringsC-Style

Stage 6: C-Style String Handling (char[])

A guide to low-level string processing as null-terminated character arrays, following exam constraints.

3 min read
Back to blog
wleci.pl

Full-stack Developer

I build modern web applications with passion for clean code and good design.

[email protected]
Poland

Navigation

  • Home
  • About
  • Projects
  • Blog
  • Contact

Services

  • Web applications
  • Websites
  • API & Backend
  • Consulting

Technologies

  • React / Next.js
  • TypeScript
  • Node.js
  • PostgreSQL

Social

© 2026 wleci.pl. All rights reserved.

Privacy policy•Terms of service

Made with in Poland