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
Dereferencing is a core part of C and C++ because pointers are used for:
- passing data efficiently to functions
- modifying variables through addresses
- working with arrays and strings
- dynamic memory allocation
- low-level system programming
- building data structures like linked lists and trees
Without understanding dereferencing, pointer-based code is very hard to read and write.
Important distinction
The * symbol is used in two related but different ways:
- In a declaration, it means "this variable is a pointer"
int* p;
- In an expression, it means "dereference this pointer"
*p
The meaning depends on where it appears.
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
#include <iostream>
using namespace std;
int main() {
int x = 10;
int* p = &x;
*p = 25;
cout << x << "\n"; // 25
cout << *p << "\n"; // 25
}
Explanation
Because p points to x, assigning to *p changes x itself.
Example 3: Dereferencing in a function
#include <iostream>
using namespace std;
void addOne(int* p) {
*p = *p + 1;
}
int main() {
int number = 7;
addOne(&number);
cout << number << "\n"; // 8
}
Explanation
addOnereceives a pointer to anint*paccesses the original integer- the function updates the caller's variable through dereferencing
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 tox
3. int y = *p;
The program dereferences p.
- it goes to the address inside
p - finds the value stored there
- that value is
5 ybecomes5
Now:
x = 5y = 5
4. *p = 12;
The program dereferences p again, but this time on the left side of an assignment.
- it goes to the address stored in
p - writes
12to that location - since that location belongs to
x,xbecomes12
Now:
x = 12y = 5
5. Output
cout << x << " " << y << "\n";
This prints:
12 5
y stayed 5 because it stored a copied value. But x changed because *p = 12 modified the original memory location.
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.
5. Interfacing with systems or libraries
Operating systems, embedded software, and C APIs frequently pass pointers so code can read or write data directly.
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] = {1, 2, 3};
for (int* p = arr; p < arr + 3; ++p) {
cout << *p << "\n";
}
Modifying shared state carefully
A pointer can give access to a shared object, so dereferencing can change program state. In real projects, this is one reason developers prefer clear naming and defensive checks.
Member access through pointers
When a pointer points to a struct or object, developers often use ->.
struct User {
int age;
};
User u{30};
User* p = &u;
cout << p->age << "\n";
This is equivalent to:
cout << (*p).age << "\n";
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:
int x = 5;
int* p = x;
Problem
p needs an address, but x is a value.
Fix
Use &x.
int x = 5;
int* p = &x;
4. Thinking *p is a copy when writing
Broken understanding:
int x = 5;
int* p = &x;
*p = 9;
Some beginners think only *p changes. In reality, x changes because *p refers to x.
5. Forgetting parentheses with pointers to structs or objects
Broken code:
cout << *p.age << "\n";
Problem
Operator precedence is wrong.
Fix
Use either:
cout << (*p).age << "\n";
or the simpler form:
cout << p->age << "\n";
6. Dereferencing after delete
Broken code:
int* p = new int(5);
delete p;
cout << *p << "\n";
Problem
After delete, the pointer no longer refers to valid owned memory.
Fix
Do not use a pointer after deleting it. Often set it to nullptr.
int* p = new int(5);
delete p;
p = nullptr;
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 | Access a member directly on an object |
Dereferencing vs getting an address
int x = 10;
int* p = &x;
&xmeans "where isxstored?"*pmeans "what value is stored there?"
These are opposite directions:
&goes from value to address*goes from address to value
*p vs p
| Expression | What it represents |
|---|---|
p | the address |
*p | the value at that address |
(*p).member vs p->member
These are equivalent when p is a pointer to a struct or class object.
(*p).age
p->age
Most programmers use -> because it is easier to read.
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
- Do not dereference uninitialized pointers
- Do not dereference
nullptrorNULL - Do not dereference a pointer after
delete - Make sure the pointer points to a valid object before using
*p
Helpful reading rule
If p points to x, then:
*p
can be read as:
- "the value pointed to by
p" - "the thing at address
p"
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?
Yes. Both mean dereference p and then access member.
Do arrays use dereferencing too?
Yes. Pointer arithmetic and dereferencing are closely related to array access. For example, arr[1] is equivalent to *(arr + 1).
Does dereferencing copy the value?
Reading through *p gives you the current value at that memory location. If you assign that to another variable, that new variable gets a copy. But writing to *p changes the original object.
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
2D Array Loop Order and Cache Performance in C
Learn why swapping nested loops changes 2D array performance in C, using row-major memory layout, cache locality, and practical benchmarks.
Array-to-Pointer Conversion in C and C++ Explained
Learn what array-to-pointer conversion means in C and C++, how array decay works, and how it differs from a pointer to an array.
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.