Bird
Raised Fist0
C++programming~10 mins

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

Choose your learning style10 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.

Practice

(1/5)
1. Which of the following statements correctly describes a reference in C++?
easy
A. A reference is an alias for another variable and cannot be changed to refer to another variable.
B. A reference holds the memory address of a variable and can be reassigned.
C. A reference can be null and does not need to be initialized.
D. A reference is a pointer that requires explicit dereferencing.

Solution

  1. Step 1: Understand what a reference is

    A reference acts as another name for an existing variable and must be initialized when declared.
  2. Step 2: Compare with pointers

    Unlike pointers, references cannot be changed to refer to another variable or be null.
  3. Final Answer:

    A reference is an alias for another variable and cannot be changed to refer to another variable. -> Option A
  4. Quick Check:

    Reference = alias, fixed binding [OK]
Hint: References alias variables and never change target [OK]
Common Mistakes:
  • Thinking references can be null
  • Confusing references with pointers
  • Assuming references can be reassigned
2. Which of the following is the correct syntax to declare a pointer to an integer in C++?
easy
A. int ptr;
B. int *ptr;
C. int &ptr;
D. int ptr*&;

Solution

  1. Step 1: Recall pointer declaration syntax

    In C++, a pointer to an int is declared using an asterisk (*) before the variable name.
  2. Step 2: Analyze options

    int *ptr; uses "int *ptr;" which correctly declares a pointer to int. int ptr; declares an int variable, B declares a reference, and D is invalid syntax.
  3. Final Answer:

    int *ptr; -> Option B
  4. Quick Check:

    Pointer syntax = int *ptr [OK]
Hint: Pointer declaration uses * before variable name [OK]
Common Mistakes:
  • Omitting the * for pointer declaration
  • Using & which declares a reference
  • Combining * and & incorrectly
3. What will be the output of the following C++ code?
int x = 10;
int &ref = x;
int *ptr = &x;
ref = 20;
*ptr = 30;
std::cout << x << std::endl;
medium
A. 30
B. 20
C. 10
D. Compilation error

Solution

  1. Step 1: Trace reference assignment

    ref is a reference to x, so ref = 20 sets x to 20.
  2. Step 2: Trace pointer assignment

    ptr points to x, so *ptr = 30 sets x to 30.
  3. Final Answer:

    30 -> Option A
  4. Quick Check:

    Reference and pointer both modify x = 30 [OK]
Hint: Both ref and *ptr change x directly [OK]
Common Mistakes:
  • Thinking ref and ptr are independent variables
  • Confusing pointer dereference syntax
  • Assuming ref can be reassigned
4. Identify the error in the following code snippet:
int a = 5;
int &ref;
ref = a;
std::cout << ref << std::endl;
medium
A. Cannot assign a variable to a reference.
B. Missing * for pointer declaration.
C. Reference must be initialized when declared.
D. Reference cannot be used with std::cout.

Solution

  1. Step 1: Check reference declaration rules

    References must be initialized at the time of declaration; they cannot be declared without initialization.
  2. Step 2: Analyze the code

    int &ref; is invalid because ref is not initialized. Assigning ref = a; later is not initialization but assignment.
  3. Final Answer:

    Reference must be initialized when declared. -> Option C
  4. Quick Check:

    Reference declaration requires immediate initialization [OK]
Hint: References need initialization on declaration [OK]
Common Mistakes:
  • Thinking references can be declared then assigned
  • Confusing assignment with initialization
  • Assuming pointers and references behave the same
5. Given the following code, what will be the output?
int x = 5;
int y = 10;
int *ptr = &x;
int &ref = y;
ptr = &y;
ref = *ptr + 5;
std::cout << x << ", " << y << std::endl;
hard
A. Compilation error
B. 5, 10
C. 5, 20
D. 5, 15

Solution

  1. Step 1: Trace pointer reassignment

    ptr initially points to x, then is reassigned to point to y.
  2. Step 2: Trace reference and assignment

    ref is a reference to y. The statement ref = *ptr + 5 means y = y + 5 (since ptr points to y).
  3. Step 3: Final values

    x remains 5, y becomes 10 + 5 = 15.
  4. Final Answer:

    5, 15 -> Option D
  5. Quick Check:

    Pointer changes target; reference updates y = 15 [OK]
Hint: Pointer can change target; reference fixed to y [OK]
Common Mistakes:
  • Assuming reference ref points to x
  • Forgetting pointer reassignment
  • Confusing reference assignment with pointer assignment
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard