Question

Dereferencing a Pointer in C and C++ Explained with Examples

ccpppointersdereferencec++-faq

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;
  • x stores the value 10
  • &x means "the address of x"
  • p stores that address
  • *p means "the value stored at the address inside p"

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:

  1. In a declaration, it means "this variable is a pointer"
int* p;
  1. 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:

  • x is a house containing 10
  • p is a note that says where x lives
  • *p means "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.

Take Quiz

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

  • x stores 42
  • p stores the address of x
  • *p follows 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

  • addOne receives a pointer to an int
  • *p accesses 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.

  • x contains 5

2. int* p = &x;

A pointer named p is created.

  • &x gets the address of x
  • p stores that address
  • p now points to x

3. int y = *p;

The program dereferences p.

  • it goes to the address inside p
  • finds the value stored there
  • that value is 5
  • y becomes 5

Now:

  • x = 5
  • y = 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 12 to that location
  • since that location belongs to x, x becomes 12

Now:

  • x = 12
  • y = 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.

Take Quiz

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

ConceptSyntaxMeaning
Address-of&xGet the memory address of x
Pointer declarationint* pDeclare p as a pointer to int
Dereference*pAccess the value at the address stored in p
Member access through pointerp->ageAccess a member through a pointer
Member access through objectobj.ageAccess a member directly on an object

Dereferencing vs getting an address

int x = 10;
int* p = &x;
  • &x means "where is x stored?"
  • *p means "what value is stored there?"

These are opposite directions:

  • & goes from value to address
  • * goes from address to value

*p vs p

ExpressionWhat it represents
pthe address
*pthe 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.

Take Quiz

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 nullptr or NULL
  • 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.

Take Quiz

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
Take Quiz

Keep learning

Related questions

View more