0
0
C++programming~10 mins

Reference vs pointer in C++ - Visual Side-by-Side Comparison

Choose your learning style9 modes available
Concept Flow - Reference vs pointer
Declare int x = 10
Create pointer p -> x
Create reference r = x
Modify *p or r
Observe x changes
Pointer can be reassigned
Reference cannot be reassigned
End
This flow shows declaring a variable, then creating a pointer and a reference to it, modifying values through them, and the key difference that pointers can be reassigned but references cannot.
Execution Sample
C++
int x = 10;
int* p = &x;
int& r = x;
*p = 20;
r = 30;
p = nullptr;
This code creates an int variable, a pointer to it, and a reference to it, then modifies the value through pointer and reference, and finally sets pointer to null.
Execution Table
StepCode LineVariable StatesActionOutput/Effect
1int x = 10;x=10Declare x with value 10x is 10
2int* p = &x;x=10, p->xPointer p points to xp points to x (10)
3int& r = x;x=10, p->x, r=xReference r aliases xr refers to x (10)
4*p = 20;x=20, p->x, r=xChange value via pointerx changes to 20
5r = 30;x=30, p->x, r=xChange value via referencex changes to 30
6p = nullptr;x=30, p=nullptr, r=xPointer p set to nullp no longer points to x
7Endx=30, p=nullptr, r=xEnd of codeFinal x is 30
💡 Execution ends after pointer is set to nullptr; reference remains bound to x.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4After Step 5After Step 6Final
xundefined10101020303030
pundefinedundefinedaddress of xaddress of xaddress of xaddress of xnullptrnullptr
rundefinedundefinedundefinedalias to xalias to xalias to xalias to xalias to x
Key Moments - 2 Insights
Why can the pointer 'p' be set to nullptr but the reference 'r' cannot be changed to refer to another variable?
Pointers store memory addresses and can be reassigned to point elsewhere or null. References are aliases fixed to the initial variable and cannot be reseated, as shown in execution_table step 6 where p changes but r stays bound to x.
When we change the value via '*p' or 'r', why does 'x' change?
Both pointer dereference '*p' and reference 'r' access the same memory location as 'x'. So modifying through either changes the original variable 'x', as seen in steps 4 and 5.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 4. What is the value of 'x' after '*p = 20;'?
A10
B20
C30
Dnullptr
💡 Hint
Check the 'Variable States' column at step 4 in execution_table.
At which step does the pointer 'p' stop pointing to 'x'?
AStep 6
BStep 3
CStep 4
DStep 5
💡 Hint
Look for when 'p' is set to nullptr in the execution_table.
If we tried to make 'r' refer to another variable after step 3, what would happen?
Ar would refer to the new variable
Br would become a pointer
CCompilation error or not allowed
Dr would become nullptr
💡 Hint
References cannot be reassigned after initialization, see key_moments explanation.
Concept Snapshot
Reference vs Pointer in C++:
- Pointer stores address, can be nullptr or reassigned.
- Reference is an alias, must be initialized, cannot be changed.
- Modify value via *pointer or reference changes original variable.
- Pointer can be null, reference always refers to valid object.
- Use references for safer aliasing, pointers for flexible address handling.
Full Transcript
This visual execution traces C++ code showing the difference between pointers and references. We start by declaring an integer variable x with value 10. Then we create a pointer p that points to x, and a reference r that aliases x. Changing the value through *p or r updates x. Later, the pointer p is set to nullptr, showing it can be reassigned or null, while the reference r remains bound to x. The variable tracker shows how x changes from 10 to 20 to 30, p points to x then becomes nullptr, and r always aliases x. Key moments clarify that pointers can be reassigned but references cannot, and both access the same memory. The quiz tests understanding of variable states at each step. This helps beginners see how pointers and references behave differently in memory and code.