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
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 elements3. 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
#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
sizeof(int) = 4
p = 0x7ffee2a1b0c0 -> 10
p + 1 = 0x7ffee2a1b0c4 -> 20
p + 2 = 0x7ffee2a1b0c8 -> 30
After p++, *p = 206. Key Takeaways
p + nmoves the pointer forward byn * sizeof(pointee type)bytes, notnbytes.- 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
1. If `int* p` points to an address `1000` and `sizeof(int)` is 4, what address does `p + 1` hold?
2. For a `double* q` with `sizeof(double) == 8`, what does `q + 2` add to the address?
3. What does subtracting two `int*` pointers, `q - p`, that point into the same array return?
4. Which operation on two pointers is undefined/not allowed in C++?
5. After `int* p = arr; p++;` where `arr` is `int[5]`, what does `p` now point to?
Was this page helpful?
You May Also Like
Pointers in C++
Learn what a pointer is, how to declare one, and how the `&` and `*` operators let you access and manipulate memory addresses directly.
Pointers and Arrays in C++
See how an array name decays into a pointer to its first element, and how `arr[i]` and `*(arr + i)` are two ways of writing the same thing.
Arrays in C++
Learn what an array is in C++, how to declare and access a fixed-size collection of same-type elements, and why zero-based indexing matters.
Related Reading
Related Study Notes in Programming
Browse all study notesApache Spark Study Notes
Programming · 30 topics
ProgrammingApache Flink Study Notes
Programming · 30 topics
ProgrammingHadoop Study Notes
Programming · 30 topics
ProgrammingSnowflake Study Notes
Programming · 30 topics
ProgrammingApache Airflow Study Notes
Programming · 30 topics
Programmingdbt (Data Build Tool) Study Notes
Programming · 30 topics