Bird
Raised Fist0
C++programming~10 mins

Pointer and variable relationship in C++ - Step-by-Step Execution

Choose your learning style10 modes available
Concept Flow - Pointer and variable relationship
Declare variable x
↓
Assign value to x
↓
Declare pointer p
↓
Assign address of x to p
↓
Access value via *p
↓
Modify x via *p
↓
End
This flow shows how a pointer stores the address of a variable and can be used to access or modify that variable.
Execution Sample
C++
int x = 10;
int* p = &x;
*p = 20;
std::cout << x;
This code creates an integer variable x, a pointer p to x, changes x through p, and prints x.
Execution Table
StepActionVariable xPointer pDereferenced *pOutput
1Declare x and assign 1010undefinedundefined
2Declare pointer p and assign address of x10address_of_x10
3Assign 20 to *p (changes x)20address_of_x20
4Print x20address_of_x2020
💡 Program ends after printing the updated value of x.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3Final
xundefined10102020
pundefinedundefinedaddress_of_xaddress_of_xaddress_of_x
Key Moments - 3 Insights
Why does changing *p also change x?
Because p stores the address of x, *p accesses the value at that address, so modifying *p changes x directly (see Step 3 in execution_table).
What does *p mean in this code?
*p means 'the value at the address stored in p'. Since p points to x, *p is the same as x (see Step 2 and 3).
Why do we use &x when assigning to p?
&x means 'the address of x'. We assign this address to p so p points to x (see Step 2).
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table, what is the value of x after Step 3?
A10
B20
Cundefined
Daddress_of_x
💡 Hint
Check the 'Variable x' column at Step 3 in the execution_table.
At which step does pointer p get assigned the address of x?
AStep 1
BStep 3
CStep 2
DStep 4
💡 Hint
Look at the 'Action' column in execution_table for when p is assigned.
If we change *p = 30; at Step 3, what will be printed at Step 4?
A30
B10
C20
Daddress_of_x
💡 Hint
Changing *p changes x, so check the 'Variable x' value after Step 3.
Concept Snapshot
Pointer and variable relationship in C++:
- Declare variable: int x = value;
- Declare pointer: int* p = &x; (store address of x)
- Access value via pointer: *p
- Changing *p changes x directly
- &x gives address of x
Pointers hold addresses, *p accesses the value at that address.
Full Transcript
This example shows how a pointer relates to a variable in C++. First, we declare an integer variable x and assign it the value 10. Then, we declare a pointer p and assign it the address of x using &x. The pointer p now points to x. When we assign 20 to *p, we change the value of x through the pointer. Finally, printing x shows the updated value 20. The key idea is that pointers store addresses, and dereferencing a pointer (*p) accesses or modifies the value at that address.

Practice

(1/5)
1. What does a pointer variable in C++ store?
easy
A. The value of another variable
B. The memory address of another variable
C. The size of a variable
D. The type of a variable

Solution

  1. Step 1: Understand what a pointer is

    A pointer is a variable that stores the address of another variable, not the value itself.
  2. Step 2: Differentiate pointer from normal variables

    Normal variables store values directly, but pointers store where those values are located in memory.
  3. Final Answer:

    The memory address of another variable -> Option B
  4. Quick Check:

    Pointer = address storage [OK]
Hint: Pointers always hold addresses, not values [OK]
Common Mistakes:
  • Confusing pointer value with variable value
  • Thinking pointer stores variable type
  • Assuming pointer stores variable size
2. Which of the following is the correct way to declare a pointer to an integer in C++?
easy
A. int ptr*;
B. *int ptr;
C. int *ptr;
D. ptr int*;

Solution

  1. Step 1: Recall pointer declaration syntax

    In C++, to declare a pointer to an int, you write the type followed by an asterisk and the pointer name.
  2. Step 2: Check each option

    Only 'int *ptr;' matches the correct syntax. Others have misplaced asterisks or wrong order.
  3. Final Answer:

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

    Pointer declaration = type *name [OK]
Hint: Pointer declaration: type *name; [OK]
Common Mistakes:
  • Placing * after variable name
  • Using * before type keyword
  • Mixing order of type and pointer symbol
3. What is the output of the following code?
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
medium
A. 20
B. 10
C. Address of x
D. Compilation error

Solution

  1. Step 1: Understand pointer assignment

    Pointer p stores the address of x. Using *p = 20 changes the value at that address, so x becomes 20.
  2. Step 2: Output the value of x

    After changing *p, printing x outputs 20.
  3. Final Answer:

    20 -> Option A
  4. Quick Check:

    Pointer dereference changes variable value [OK]
Hint: Changing *p changes x's value [OK]
Common Mistakes:
  • Thinking *p = 20 changes pointer, not value
  • Expecting output to be address
  • Confusing pointer and variable values
4. Identify the error in the following code:
int x = 5;
int *p;
*p = x;
std::cout << *p << std::endl;
medium
A. Pointer p is not initialized before dereferencing
B. Variable x is not declared
C. Incorrect pointer declaration syntax
D. Missing semicolon after declaration

Solution

  1. Step 1: Check pointer initialization

    Pointer p is declared but not assigned an address before using *p = x; which causes undefined behavior.
  2. Step 2: Understand dereferencing uninitialized pointer

    Dereferencing p without initialization leads to runtime error or crash.
  3. Final Answer:

    Pointer p is not initialized before dereferencing -> Option A
  4. Quick Check:

    Always initialize pointers before dereference [OK]
Hint: Initialize pointers before use to avoid errors [OK]
Common Mistakes:
  • Dereferencing uninitialized pointers
  • Assuming pointer auto-initializes
  • Ignoring runtime errors from bad pointers
5. Given the code below, what will be the output?
int a = 3, b = 4;
int *p = &a;
int *q = &b;
int *temp = p;
p = q;
q = temp;
std::cout << *p << " " << *q << std::endl;
hard
A. 3 4
B. 3 3
C. 4 4
D. 4 3

Solution

  1. Step 1: Understand pointer swapping

    Initially, p points to a (3), q points to b (4). temp stores p (address of a).
  2. Step 2: Swap pointers

    p = q makes p point to b (4), q = temp makes q point to a (3).
  3. Step 3: Print values pointed by p and q

    *p is 4, *q is 3, so output is "4 3".
  4. Final Answer:

    4 3 -> Option D
  5. Quick Check:

    Pointer swap changes what they point to [OK]
Hint: Swapping pointers swaps pointed values [OK]
Common Mistakes:
  • Confusing pointer values with pointed values
  • Mixing up which pointer points where after swap
  • Assuming values themselves swap
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