Question
In C and C++, what does it mean to dereference a pointer? Please explain the idea clearly and include a simple example showing how it works.
Short Answer
By the end of this page, you will understand what a pointer stores, what dereferencing means, how the * operator is used to access the value at an address, and how this appears in real C and C++ programs. You will also learn common mistakes, how to read pointer syntax, and how to safely use dereferencing in beginner-friendly code.
Concept
A pointer is a variable that stores the memory address of another value.
Dereferencing a pointer means:
- going to the memory address stored in the pointer
- accessing or modifying the value that lives at that address
In C and C++, the dereference operator is *.
For example:
int x = 10;
int* p = &x;
xstores the value10&xmeans "the address ofx"pstores that address*pmeans "the value stored at the address insidep"
So if p points to x, then *p refers to x.
That means this is true:
*p = 25;
After this line, x becomes 25.
Why this matters
Mental Model
Think of a pointer like a piece of paper with a house address written on it.
- The house is the actual variable in memory.
- The address on the paper is the pointer's value.
- Dereferencing means going to that house and seeing what is inside.
Example:
xis a house containing10pis a note that says wherexlives*pmeans "go to that house and open the door"
If you write through *p, you are not changing the note. You are changing what is inside the house.
That is why:
*p = 99;
changes the original variable being pointed to.
Syntax and Examples
Core syntax
int x = 10;
int* p = &x; // p stores the address of x
int y = *p; // dereference p to get the value at that address
Example 1: Reading a value through a pointer
#include <iostream>
using namespace std;
int main() {
int x = 42;
int* p = &x;
cout << x << "\n"; // 42
cout << p << "\n"; // memory address of x
cout << *p << "\n"; // 42
}
Explanation
xstores42pstores the address ofx*pfollows that address and reads the value there
Example 2: Modifying a value through a pointer
Step by Step Execution
Consider this small example:
#include <iostream>
using namespace std;
int main() {
int x = 5;
int* p = &x;
int y = *p;
*p = 12;
cout << x << " " << y << "\n";
}
Step-by-step trace
1. int x = 5;
A normal integer variable named x is created.
xcontains5
2. int* p = &x;
A pointer named p is created.
&xgets the address ofxpstores that addresspnow points to
Real World Use Cases
Dereferencing appears in many real programs.
1. Updating values inside functions
In C, functions often use pointers when they need to modify caller-owned data.
void setZero(int* value) {
*value = 0;
}
2. Working with arrays
Array names often behave like pointers to the first element.
int arr[3] = {10, 20, 30};
int* p = arr;
// *p is arr[0]
// *(p + 1) is arr[1]
Dereferencing is how pointer-based array access works.
3. Dynamic memory
When memory is allocated dynamically, you usually access it through a pointer.
int* p = new int(50);
cout << *p << "\n";
delete p;
4. Data structures
Linked lists, trees, graphs, and many low-level containers rely on pointers.
node->next
This is closely related to dereferencing. is shorthand for dereferencing a pointer to an object and accessing one of its members.
Real Codebase Usage
In real codebases, dereferencing is rarely taught in isolation. It usually appears as part of common programming patterns.
Guard clauses before dereferencing
Developers often check for nullptr or NULL before using a pointer.
void printValue(const int* p) {
if (p == nullptr) {
return;
}
cout << *p << "\n";
}
This avoids invalid memory access.
Output parameters in C-style APIs
Many C functions return results through pointer parameters.
void divide(int a, int b, int* result) {
if (b == 0 || result == NULL) {
return;
}
*result = a / b;
}
Iterating with pointers
Some performance-sensitive code uses pointers directly instead of indexes.
int arr[3] = {, , };
(* p = arr; p < arr + ; ++p) {
cout << *p << ;
}
Common Mistakes
1. Dereferencing an uninitialized pointer
Broken code:
int* p;
cout << *p << "\n";
Problem
p does not point to a valid location. Dereferencing it causes undefined behavior.
Fix
Always initialize pointers.
int x = 10;
int* p = &x;
cout << *p << "\n";
2. Dereferencing nullptr or NULL
Broken code:
int* p = nullptr;
cout << *p << "\n";
Problem
A null pointer points to no valid object.
Fix
Check before dereferencing.
if (p != nullptr) {
cout << *p << "\n";
}
3. Confusing address-of and dereference
Broken code:
Comparisons
Related operators and ideas
| Concept | Syntax | Meaning |
|---|---|---|
| Address-of | &x | Get the memory address of x |
| Pointer declaration | int* p | Declare p as a pointer to int |
| Dereference | *p | Access the value at the address stored in p |
| Member access through pointer | p->age | Access a member through a pointer |
| Member access through object | obj.age |
Cheat Sheet
Quick reference
Pointer declaration
int* p;
p is a pointer to int.
Store an address in a pointer
int x = 10;
int* p = &x;
Dereference a pointer
cout << *p;
Reads the value at the address stored in p.
Write through a pointer
*p = 20;
Changes the original value being pointed to.
Address-of vs dereference
&x // address of x
*p // value at pointer p
Pointer to struct/object member
p->member
(*p).member
These mean the same thing.
Safety rules
FAQ
What does dereferencing mean in C or C++?
Dereferencing means accessing the value stored at the memory address held by a pointer. In code, this is usually done with *pointer.
What is the difference between p and *p?
p is the address stored in the pointer. *p is the value found at that address.
Why is & used when assigning a pointer?
& gets the address of a variable. A pointer stores addresses, so int* p = &x; gives p the location of x.
Can dereferencing change the original variable?
Yes. If a pointer points to a variable, assigning to *p changes that original variable.
Is dereferencing a null pointer allowed?
No. Dereferencing nullptr in C++ or NULL in C is invalid and causes undefined behavior.
Is p->member the same as (*p).member?
Mini Project
Description
Build a small program that demonstrates how dereferencing lets a function modify a variable from the caller. This is useful because many C and C++ programs use pointers to update values without returning them directly.
Goal
Create a program that changes a number through a pointer and prints the value before and after the change.
Requirements
- Declare an integer variable in
main - Create a function that accepts a pointer to an integer
- Dereference the pointer inside the function and modify the value
- Print the value before and after the function call
Keep learning
Related questions
Building More Fault-Tolerant Embedded C++ Applications for Radiation-Prone ARM Systems
Learn practical C++ and compile-time techniques to reduce soft-error damage in embedded ARM systems exposed to radiation.
C printf Format Specifier for bool: How to Print Boolean Values
Learn how to print bool values in C with printf, why no %b/%B specifier exists, and the common patterns to print true/false or 0/1.
Calling C or C++ from Python: Building Python Bindings
Learn the quickest ways to call C or C++ from Python, including ctypes, C extensions, Cython, and binding tools with practical examples.