100% Free Forever
AI-Powered Learning
Industry Expert Content
Certificates & Badges
Learn At Your Own Pace
C++

Pointer Arithmetic in C++

Understand how adding or subtracting integers from a pointer moves it by whole elements, scaled by the size of the pointee type.

PointersIntermediate7 min readJul 7, 2026
Analogies

1. Intro

Pointer arithmetic refers to performing arithmetic operations — addition, subtraction, increment, decrement — directly on pointer variables. Unlike ordinary arithmetic on integers, pointer arithmetic is always scaled by the size of the type the pointer points to, which makes it extremely useful for stepping through arrays element by element.

🏏

Cricket analogy: Just as a scorer moves exactly one over ahead on the scorecard rather than one random line, pointer arithmetic advances a pointer by whole elements, not raw bytes, keeping it aligned on real data like Virat Kohli's over-by-over tally.

2. Syntax

cpp
p + n;   // moves the pointer forward by n elements
p - n;   // moves the pointer backward by n elements
p++;     // moves forward by 1 element
p--;     // moves backward by 1 element
q - p;   // difference between two pointers, in elements

3. Explanation

When you write p + 1 for a pointer p, C++ does not simply add 1 byte to the address stored in p. Instead, it adds 1 * sizeof(the pointed-to type) bytes. For example, if p is an int* and sizeof(int) is 4 bytes, then p + 1 moves the address forward by 4 bytes — landing exactly on the next int in memory. If p were a double* (8 bytes), p + 1 would move forward by 8 bytes instead. This scaling is what makes p++ correctly walk to the *next array element*, regardless of the element's size. Subtracting two pointers of the same type (q - p) gives the number of elements between them, not the raw byte difference. Only addition/subtraction of integers to pointers, and subtraction between two pointers into the same array, are well-defined; multiplying or adding two pointers is not allowed.

🏏

Cricket analogy: If a scoreboard entry for each ball takes 4 digits of space, moving p + 1 on the ball-by-ball log jumps 4 digits forward to land exactly on Jasprit Bumrah's next delivery, not into the middle of one.

Pointer arithmetic scales by the pointee type's size: p + n advances the address by n * sizeof(*p) bytes, not by n bytes. This is one of the most commonly tested facts about pointers.

4. Example

cpp
#include <iostream>
using namespace std;

int main() {
    int arr[4] = {10, 20, 30, 40};
    int* p = arr;   // points to arr[0]

    cout << "sizeof(int) = " << sizeof(int) << endl;
    cout << "p       = " << p << " -> " << *p << endl;
    cout << "p + 1   = " << (p + 1) << " -> " << *(p + 1) << endl;
    cout << "p + 2   = " << (p + 2) << " -> " << *(p + 2) << endl;

    p++;
    cout << "After p++, *p = " << *p << endl;

    return 0;
}

5. Output

text
sizeof(int) = 4
p       = 0x7ffee2a1b0c0 -> 10
p + 1   = 0x7ffee2a1b0c4 -> 20
p + 2   = 0x7ffee2a1b0c8 -> 30
After p++, *p = 20

6. Key Takeaways

  • p + n moves the pointer forward by n * sizeof(pointee type) bytes, not n bytes.
  • Pointer arithmetic is only well-defined within (or one-past) the same array/allocation.
  • Subtracting two pointers of the same type gives the number of elements between them, not raw bytes.
  • p++/p-- step exactly one element at a time, which is what makes pointer-based array traversal work.

Practice what you learned

Was this page helpful?

Topics covered

#CStudyNotes#Programming#PointerArithmeticInC#Pointer#Arithmetic#Syntax#Explanation#StudyNotes#SkillVeris#ExamPrep