C++ Pointer Dereferencing
In this page:
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 *
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
int value = 10;
int *ptr = &value;
value = 20;
std::cout << *ptr << std::endl; // reflects the latest value
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
#include <iostream>
int main() {
double value = 3.14;
double *ptr = &value;
std::cout << *ptr << std::endl; // reads 8 bytes as a double
return 0;
}
Login to try C/C++/Java/PHP code in the editor
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
// 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;
}
Login to try C/C++/Java/PHP code in the editor
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 ->
// 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;
}
Login to try C/C++/Java/PHP code in the editor
- Dereferencing a null or uninitialized pointer, which crashes.
- Forgetting
*and printing the address instead of the value. - Dereferencing a
void*without casting it first.
Chapter Quiz — Complete all 11 topics to unlock
0/11 topics done
Complete these topics first: