Bird
Raised Fist0
Cprogramming~10 mins

Address and dereference operators - Step-by-Step Execution

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

Practice

(1/5)
1. What does the & operator do in C?
easy
A. It gives the memory address of a variable.
B. It multiplies two numbers.
C. It dereferences a pointer to get the value.
D. It declares a pointer variable.

Solution

  1. Step 1: Understand the & operator

    The & operator is used to get the address of a variable in memory.
  2. Step 2: Differentiate from other operators

    It does not multiply, dereference, or declare pointers; those are different operations.
  3. Final Answer:

    It gives the memory address of a variable. -> Option A
  4. Quick Check:

    & means address-of [OK]
Hint: Remember & means 'address of' variable [OK]
Common Mistakes:
  • Confusing & with multiplication operator
  • Thinking & dereferences a pointer
  • Mixing & with pointer declaration
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. int *ptr;

Solution

  1. Step 1: Recall pointer declaration syntax

    In C, a pointer to an int is declared as int *ptr; where * indicates pointer type.
  2. Step 2: Check other options for syntax errors

    int &ptr; is invalid in C (it's C++ reference), int ptr*; and *int ptr; are incorrect syntax.
  3. Final Answer:

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

    Pointer declaration uses * before variable name [OK]
Hint: Pointer declaration: type *name; [OK]
Common Mistakes:
  • Using & instead of * for pointers
  • Placing * after variable name incorrectly
  • Confusing C++ references with pointers
3. What is the output of this code?
int x = 10;
int *p = &x;
printf("%d", *p);
medium
A. 10
B. The memory address of x
C. Garbage value
D. Compilation error

Solution

  1. Step 1: Understand pointer assignment

    Pointer p stores the address of x using &x.
  2. Step 2: Dereference pointer to get value

    *p accesses the value at the address stored in p, which is 10.
  3. Final Answer:

    10 -> Option A
  4. Quick Check:

    *p gives value at address [OK]
Hint: *p gives value stored at pointer address [OK]
Common Mistakes:
  • Printing pointer variable instead of dereferenced value
  • Confusing & and * operators
  • Expecting address instead of value
4. Identify the error in this code snippet:
int x = 5;
int *p;
*p = x;
printf("%d", *p);
medium
A. Incorrect use of & operator.
B. Pointer p is not initialized before dereferencing.
C. Variable x is not declared.
D. Missing semicolon after declaration.

Solution

  1. Step 1: Check pointer initialization

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

    Dereferencing p without initialization causes undefined behavior or crash.
  3. Final Answer:

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

    Always assign pointer before dereferencing [OK]
Hint: Initialize pointer before using * to access value [OK]
Common Mistakes:
  • Dereferencing uninitialized pointer
  • Confusing assignment with address-of
  • Ignoring pointer initialization warnings
5. Given the code below, what will be the output?
int a = 3, b = 7;
int *p = &a;
*p = *p + 4;
p = &b;
*p = *p - 2;
printf("%d %d", a, b);
hard
A. 7 3
B. 3 5
C. 7 5
D. 7 7

Solution

  1. Step 1: Modify value pointed by p initially

    p points to a. *p = *p + 4; changes a from 3 to 7.
  2. Step 2: Change pointer to point to b and modify

    p = &b; points to b. *p = *p - 2; changes b from 7 to 5.
  3. Step 3: Print final values

    a is 7 and b is 5.
  4. Final Answer:

    7 5 -> Option C
  5. Quick Check:

    Pointer changes values via dereference [OK]
Hint: Track pointer target and value changes stepwise [OK]
Common Mistakes:
  • Not updating the correct variable via pointer
  • Confusing pointer assignment with value assignment
  • Ignoring pointer reassignment effects
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