0Pricing
C++ Academy · Lesson

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 — lesson 2 of 4. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C++ Academy learning path, one of 4 lessons in the course, and your progress syncs across the web and the CoddyKit app.

Reading Through a Pointer

The unary * operator reads the value the pointer points to.

int x = 42;
int* p = &x;
std::cout << *p;   // 42

Writing 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;   // 100

The 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;   // 3

Pointer 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;   // 30

Walking 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;  // 5

Comparing 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.

Frequently asked questions

Is the “Dereferencing and Pointer Arithmetic” lesson free?

Yes — the full text of “Dereferencing and Pointer Arithmetic” is free to read here on the web, and the C++ Academy course includes 4 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C++ Academy course, upgrade to CoddyKit PRO.

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; this is — lesson 2 of 4, so you can start here or from the beginning and move at your own pace.

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

  1. Raw Pointers and the Address-of Operator
  2. Dereferencing and Pointer Arithmetic
  3. nullptr and Null Pointer Safety
  4. When to Use Pointers vs References
← Back to C++ Academy