0
0
C++programming~10 mins

Address and dereference operators in C++ - Step-by-Step Execution

Choose your learning style9 modes available
Concept Flow - Address and dereference operators
Declare variable x
Use & operator to get address of x
Store address in pointer p
Use * operator to access value at address p
Read or modify value through *p
End
This flow shows how a variable's address is taken with & and accessed or changed using *.
Execution Sample
C++
int x = 10;
int* p = &x;
int y = *p;
*p = 20;
This code stores x's address in p, reads x through *p into y, then changes x via *p.
Execution Table
StepCode LineActionVariable StatesOutput/Effect
1int x = 10;Declare x and set to 10x=10x initialized to 10
2int* p = &x;Store address of x in pointer pp=&x (address), x=10p points to x
3int y = *p;Dereference p to get value of x, assign to yy=10, p=&x, x=10y gets value 10
4*p = 20;Change value at address p points to (x) to 20x=20, y=10, p=&xx updated to 20 via pointer
5EndProgram endsx=20, y=10, p=&xFinal values set
💡 Program ends after modifying x through pointer *p.
Variable Tracker
VariableStartAfter Step 1After Step 2After Step 3After Step 4Final
xundefined1010102020
pundefinedundefined&x&x&x&x
yundefinedundefinedundefined101010
Key Moments - 3 Insights
Why does *p give the value of x?
Because p stores the address of x (see step 2), *p accesses the value at that address (step 3).
What happens when we do *p = 20?
It changes the value stored at the address p points to, which is x (step 4), so x becomes 20.
Is p the value of x?
No, p holds the address of x, not x's value itself (step 2). To get x's value via p, we use *p.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution table, what is the value of y after step 3?
A20
BAddress of x
C10
DUndefined
💡 Hint
Check the 'Variable States' column at step 3 in the execution table.
At which step does the value of x change?
AStep 4
BStep 3
CStep 2
DStep 1
💡 Hint
Look for the step where '*p = 20;' is executed in the code line column.
If we changed 'int* p = &x;' to 'int* p = nullptr;', what would happen when we do '*p = 20;'?
Ax would become 20
BProgram would crash or undefined behavior
Cy would become 20
DNothing changes, safe operation
💡 Hint
Dereferencing a null pointer (*p when p is nullptr) causes a runtime error.
Concept Snapshot
Address operator (&) gets a variable's memory address.
Dereference operator (*) accesses or changes the value at that address.
Use & to assign pointer, * to read or write through pointer.
Pointers hold addresses, not values.
Changing *pointer changes the original variable.
Full Transcript
This example shows how to use the address operator & and dereference operator * in C++. First, we declare an integer x with value 10. Then, we create a pointer p that stores the address of x using &x. Next, we read the value of x through the pointer by dereferencing p with *p and assign it to y. Finally, we change the value of x by assigning 20 to *p. The variable tracker shows how x changes from 10 to 20, while y remains 10. The key moments clarify that *p accesses the value at the address stored in p, and changing *p changes x. The visual quiz tests understanding of variable values at each step and what happens if p is null. This helps beginners see how pointers work step-by-step.