0
0
Cprogramming~10 mins

Address and dereference operators - 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 on p to access value of x
Modify value via *p
Value of x changes
End
This flow shows how we get the address of a variable using & and access or change its value using *.
Execution Sample
C
int x = 10;
int *p = &x;
*p = 20;
printf("x = %d", x);
This code sets x to 10, points p to x, changes x to 20 via p, then prints x.
Execution Table
StepCode LineVariable xPointer pActionOutput
1int x = 10;10undefinedx declared and set to 10
2int *p = &x;10address_of_xp stores address of x
3*p = 20;20address_of_xvalue at p (x) changed to 20
4printf("x = %d", x);20address_of_xprint value of xx = 20
💡 Program ends after printing x = 20
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 holds 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 &x mean?
&x gives the memory address of variable x, which is stored in pointer p (see step 2 in execution_table).
Can we print *p instead of x to get the same value?
Yes, *p dereferences the pointer to get x's value, so printing *p would output the same as printing x.
Visual Quiz - 3 Questions
Test your understanding
Look at the execution_table at step 2, what does pointer p hold?
AUndefined
BThe value 10
CThe address of x
DThe value 20
💡 Hint
Check the 'Pointer p' column at step 2 in execution_table.
At which step does the value of x change from 10 to 20?
AStep 3
BStep 2
CStep 1
DStep 4
💡 Hint
Look at the 'Variable x' column in execution_table to see when it changes.
If we change *p = 30 instead of 20 at step 3, what will be printed at step 4?
Ax = 10
Bx = 30
Cx = 20
DCompilation error
💡 Hint
Changing *p changes x's value; see variable_tracker for how x changes.
Concept Snapshot
Address operator (&) gets a variable's memory address.
Dereference operator (*) accesses or changes the value at that address.
Use int *p = &x; to point p to x.
Changing *p changes x directly.
Useful for working with pointers and memory.
Full Transcript
This lesson shows how the address operator & gets the memory location of a variable, and the dereference operator * accesses or changes the value stored there. We start by declaring an integer x with value 10. Then we declare a pointer p that stores the address of x using &x. When we write *p = 20, we change the value at the address p points to, which is x. So x becomes 20. Finally, printing x shows the updated value 20. This helps understand how pointers let us work with memory directly.