wleci
AboutProjectsBlogContact
Contact
Back to blog
PointersMemory ManagementBasics

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

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

February 11, 20263 min read
Share:

Pointers: The Key to Understanding Computer Memory

Pointers are one of the most powerful and demanding elements of the C++ language. Unlike regular variables that store specific values (like numbers or characters), pointers store addresses in RAM. Understanding them is essential for working with dynamic memory allocation, arrays, and advanced data structures.


1. The Address-of Operator (&) and Pointer Declaration

Every variable in a program occupies a specific spot in memory. To find out the address where a variable is located, we use the address-of operator &.

Declaration

To declare a pointer, we use the asterisk symbol * with the data type. The pointer's type must match the type of data it points to.

cpp
int number = 100; int *ptr; // Declaration of a pointer to an integer ptr = &number; // Assigning the address of 'number' to the pointer

2. The Dereference Operator (*)

Once we have a pointer holding an address, we usually want to access the value stored at that address. We use the dereference operator * for this.

Note: The * symbol has two meanings:

  1. In declaration: int *ptr; means "I am creating a pointer."
  2. In code: *ptr = 20; means "go to the address in ptr and write 20 there."
cpp
#include <iostream> int main() { int x = 10; int *p = &x; std::cout << "Address of x: " << p << std::endl; std::cout << "Value at address p: " << *p << std::endl; // Prints 10 *p = 50; // Changing x's value via the pointer std::cout << "New value of x: " << x << std::endl; // Prints 50 return 0; }

3. Null Pointer: nullptr

A pointer that hasn't been initialized contains a random address ("garbage"). Attempting to dereference such a pointer almost always results in a Segmentation Fault. Therefore, it is best practice to initialize a pointer with nullptr.

cpp
int *p = nullptr; // Pointer points to nothing if (p != nullptr) { std::cout << *p; }

4. Pointer Arithmetic and Arrays

Arrays and pointers are closely related in C++. An array's name is actually a pointer to its first element.

  • tab is the address &tab[0]
  • *(tab + 1) is the same as tab[1]

We can move a pointer along an array by adding integers to it. C++ automatically calculates the byte offset based on the data type (e.g., for int, adding 1 moves the address by 4 bytes).

cpp
int numbers[] = {10, 20, 30}; int *ptr = numbers; // Points to numbers[0] std::cout << *ptr << " "; // 10 std::cout << *(ptr + 1) << " "; // 20

Exam Pitfalls

  1. Uninitialized Pointers: Never do *p = 5; unless you have assigned p the address of an existing variable or dynamic memory.
  2. Confusing Operators:
    • & - give me the address.
    • * - enter this address.
  3. Pointers vs. References: A reference is just an alias (another name), whereas a pointer is a separate variable storing an address. A pointer can be changed during execution to point to something else—a reference cannot.

Example: Function Using a Pointer

Instead of a reference, we can use a pointer to modify a value in a function:

cpp
void setZero(int *p) { if (p != nullptr) { *p = 0; } } int main() { int a = 100; setZero(&a); // We pass the address of variable a // a is now 0 }

You might also like

PointersMemory

Stage 11: Pointer Arithmetic in C++

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

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