← Back to C++ Course | Chapter 7: Pointers & References | Lesson 2 of 11

C++ Pointer Dereferencing

Dereferencing a pointer with the * operator follows its stored address to access the actual value stored there, letting the pointed-to data be read or modified.
Syntax
cpp
data_type *pointer_name = &variable_name;

*pointer_name;  // the value the pointer points to

The Dereference Operator *

The * operator, when applied to a pointer, follows the memory address it stores and gives access to the value found there -- this is called dereferencing the pointer.

Example: The Dereference Operator *

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	// Declare value and set it to 10
	int value = 10;
	int *ptr = &value;
	// Print to the console with cout
	std::cout << *ptr << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}

Reading a Value Through a Pointer

Once a pointer holds a valid address, dereferencing it any number of times reads the current value at that address, always reflecting the latest value even if it changed after the pointer was set.

Example: Reading a Value Through a Pointer

cpp
#include <iostream>

int main() {
	int value = 10;
	int *ptr = &value;
	value = 20;
	std::cout << *ptr << std::endl; // reflects the latest value
	return 0;
}

Dereferencing and Data Types

The type a pointer was declared with determines how many bytes are read and how the resulting value is interpreted when it's dereferenced, which is why a pointer's declared type must match what it actually points to.

Example: Dereferencing and Data Types

cpp
#include <iostream>

int main() {
	double value = 3.14;
	double *ptr = &value;
	std::cout << *ptr << std::endl; // reads 8 bytes as a double
	return 0;
}

Dereferencing a Null Pointer

Dereferencing a null pointer, one that hasn't been assigned a valid address, is undefined behavior and typically crashes the program, so a pointer should always be checked or known to be valid before dereferencing.

Example: Dereferencing a Null Pointer

cpp
// Include std::cout and std::cin
#include <iostream>

// Program execution starts in main()
int main() {
	int *ptr = nullptr;
	// Check whether ptr != nullptr
	if (ptr != nullptr) {
		// Print to the console with cout
		std::cout << *ptr << std::endl;
	} else {
		// Print to the console with cout
		std::cout << "Cannot dereference a null pointer" << std::endl;
	}
	// Return 0 to signal that the program finished successfully
	return 0;
}

Dereferencing Combined with Arrow ->

For a pointer to a struct or class, (*ptr).member and ptr->member are equivalent ways to dereference the pointer and access one of its members, with -> being the shorter, more common form.

Example: Dereferencing Combined with Arrow ->

cpp
// Include std::cout and std::cin
#include <iostream>

// Define a structure type named Point
struct Point { int x; int y; };

// Program execution starts in main()
int main() {
	Point p = {3, 4};
	Point *ptr = &p;
	// Print to the console with cout
	std::cout << (*ptr).x << " " << ptr->x << std::endl;
	// Return 0 to signal that the program finished successfully
	return 0;
}
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
Common Mistakes
  1. Dereferencing a null or uninitialized pointer, which crashes.
  2. Forgetting * and printing the address instead of the value.
  3. Dereferencing a void* without casting it first.

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.