Having a treasure map (a Pointer) is useless if you don't actually dig up the gold. Dereferencing is the act of traveling to the memory address stored inside a pointer and physically interacting with the data that lives there.
You dereference a pointer by placing the Dereference Operator (`*`) in front of it. This allows you to not only read the original variable's data, but also overwrite it directly in memory, permanently changing the original variable.
int score = 100;
int* ptr = &score; // ptr holds the address of score
// 1. Reading data via Dereferencing
// We travel to the address and print the data (100)
std::cout << *ptr << "\n";
// 2. Mutating data via Dereferencing
// We travel to the address and forcefully overwrite the data with 999
*ptr = 999;
// The original 'score' variable is now permanently 999!
std::cout << score << "\n";*). When declaring a variable (int* p), the asterisk means 'This is a pointer'. When used in front of an existing pointer (*p = 5), it is an action meaning 'Travel to this address and edit the data'.* in front of it tells the CPU to jump into protected or non-existent RAM. The OS will instantly terminate your program with an Access Violation or Segfault.What happens if you execute int a = 10; int* p = &a; int b = *p; b = 20;? Does a change to 20?
No. a remains 10. int b = *p; simply travels to a, reads the value '10', and creates a brand new, completely separate physical copy of that '10' for b. Changing b later has absolutely zero effect on a.
Passing a pointer into a function so the function can physically travel back to the main scope and alter a variable.
#include <iostream>
// The function accepts a memory address (a pointer)
void takeDamage(int* healthAddr, int damage) {
// We dereference the pointer to edit the original health!
*healthAddr = *healthAddr - damage;
}
int main() {
int playerHP = 100;
// We pass the physical address of playerHP
takeDamage(&playerHP, 25);
std::cout << "HP is now: " << playerHP << "\n"; // Prints 75!
return 0;
}Test your understanding of Dereferencing with these quick questions.
Question 1
In the code `int val = 50; int* p = &val; cout << p;`, what exactly is printed to the screen?
Having a treasure map (a Pointer) is useless if you don't actually dig up the gold. Dereferencing is the act of traveling to the memory address stored inside a pointer and physically interacting with the data that lives there.
You dereference a pointer by placing the Dereference Operator (`*`) in front of it. This allows you to not only read the original variable's data, but also overwrite it directly in memory, permanently changing the original variable.
int score = 100;
int* ptr = &score; // ptr holds the address of score
// 1. Reading data via Dereferencing
// We travel to the address and print the data (100)
std::cout << *ptr << "\n";
// 2. Mutating data via Dereferencing
// We travel to the address and forcefully overwrite the data with 999
*ptr = 999;
// The original 'score' variable is now permanently 999!
std::cout << score << "\n";*). When declaring a variable (int* p), the asterisk means 'This is a pointer'. When used in front of an existing pointer (*p = 5), it is an action meaning 'Travel to this address and edit the data'.* in front of it tells the CPU to jump into protected or non-existent RAM. The OS will instantly terminate your program with an Access Violation or Segfault.What happens if you execute int a = 10; int* p = &a; int b = *p; b = 20;? Does a change to 20?
No. a remains 10. int b = *p; simply travels to a, reads the value '10', and creates a brand new, completely separate physical copy of that '10' for b. Changing b later has absolutely zero effect on a.
Passing a pointer into a function so the function can physically travel back to the main scope and alter a variable.
#include <iostream>
// The function accepts a memory address (a pointer)
void takeDamage(int* healthAddr, int damage) {
// We dereference the pointer to edit the original health!
*healthAddr = *healthAddr - damage;
}
int main() {
int playerHP = 100;
// We pass the physical address of playerHP
takeDamage(&playerHP, 25);
std::cout << "HP is now: " << playerHP << "\n"; // Prints 75!
return 0;
}Test your understanding of Dereferencing with these quick questions.
Question 1
In the code `int val = 50; int* p = &val; cout << p;`, what exactly is printed to the screen?