Dereferencing and Pointer Arithmetic
Read memory through pointers and walk arrays with pointer arithmetic.
Dereferencing and Pointer Arithmetic is a free C++ Academy lesson on CoddyKit. This is lesson 2 of 4. Any 3 lessons of this learning path are free to read in full — after that, CoddyKit PRO unlocks every lesson, plus hands-on practice with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, and your progress syncs across the web and the CoddyKit app. The C++ Academy course includes 4 lessons in total.
Reading Through a Pointer
The unary * operator reads the value the pointer points to.
int x = 42;
int* p = &x;
std::cout << *p; // 42Writing Through a Pointer
You can also use *p on the left side of an assignment to write to the target.
*p = 100;
std::cout << x; // 100The Arrow Operator
For pointers to structs or classes, p->member is a shorthand for (*p).member. Use the arrow — it is the convention.
struct Point { int x, y; };
Point pt{3, 4};
Point* p = &pt;
std::cout << p->x; // 3Pointer Arithmetic Basics
You can add or subtract integers from pointers. The result moves in element-sized steps — not bytes.
int a[5] = {10, 20, 30, 40, 50};
int* p = a; // points to a[0]
p = p + 2; // now points to a[2]
std::cout << *p; // 30Walking an Array
Pointer arithmetic mimics indexing. a[i] and *(a + i) are identical — and p[i] works on any pointer.
int a[] = {1, 2, 3, 4};
int* p = a;
for (int i = 0; i < 4; ++i) {
std::cout << *(p + i) << " ";
}Difference of Two Pointers
Subtracting two pointers into the same array gives the element distance — type is std::ptrdiff_t.
int a[5];
int* first = a;
int* last = a + 5;
std::ptrdiff_t len = last - first; // 5Comparing Pointers
You can compare pointers for equality with == and !=, and order with < only when they point into the same array.
Iterating with Pointers
Idiomatic C-style iteration uses two pointers — begin and end.
int a[] = {1, 2, 3, 4, 5};
for (int* p = a; p != a + 5; ++p) {
std::cout << *p << " ";
}Beyond the End Is Allowed
You may compute a pointer one past the last element (the "end" pointer) — but dereferencing it is undefined.
Pitfalls of Pointer Arithmetic
Common mistakes:
- Going past the array bounds
- Mixing pointer types (
int*+char*) - Forgetting that arithmetic is in element units, not bytes
Prefer Iterators or Ranges
Modern C++ favors iterators and the ranges library over raw pointer arithmetic. Pointers remain important for C APIs and very low-level code.
Quick Check
If p is an int* pointing to the start of a 4-byte int, what does p + 1 point to?
Recap
*p reads or writes the target. p->member accesses a struct member. Pointer arithmetic moves in element-sized steps. Use it carefully — iterators and ranges are usually a safer choice in modern code.
Learn C++ with an AI tutor — free
Write and run real code in your browser, get instant help from a 24/7 AI tutor, and pick up where you left off on the web or in the app.
- Courses
- 51
- Lessons
- 203
Frequently Asked Questions
Is the “Dereferencing and Pointer Arithmetic” lesson free?
Yes — any 3 lessons of the C++ Academy learning path, including “Dereferencing and Pointer Arithmetic”, are free to read in full here on the web. After that, CoddyKit PRO unlocks every lesson, plus interactive practice with a built-in code editor and a 24/7 AI tutor. The C++ Academy course includes 4 lessons in total.
What will I learn in “Dereferencing and Pointer Arithmetic”?
Read memory through pointers and walk arrays with pointer arithmetic. You practise C++ Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C++ Academy?
No prior experience is required. C++ Academy on CoddyKit is structured for beginners through advanced learners, so you can start here or from the beginning and move at your own pace. This is lesson 2 of 4.
How long does the “Dereferencing and Pointer Arithmetic” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C++ Academy lesson?
Yes. Every C++ Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- Raw Pointers and the Address-of Operator
- Dereferencing and Pointer Arithmetic
- nullptr and Null Pointer Safety
- When to Use Pointers vs References