Bird
Raised Fist0
C++programming~20 mins

Address and dereference operators in C++ - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pointer Mastery
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pointer dereferencing
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int x = 10;
    int* p = &x;
    std::cout << *p << std::endl;
    return 0;
}
AAddress of x
B10
CCompilation error
D0
Attempts:
2 left
💡 Hint
Remember that *p gives the value stored at the address p points to.
❓ Predict Output
intermediate
2:00remaining
Value after pointer modification
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    int a = 5;
    int* ptr = &a;
    *ptr = 20;
    std::cout << a << std::endl;
    return 0;
}
A20
BAddress of a
C5
DCompilation error
Attempts:
2 left
💡 Hint
Changing *ptr changes the value of a.
❓ Predict Output
advanced
2:00remaining
Pointer arithmetic and dereferencing
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[] = {1, 2, 3, 4};
    int* p = arr;
    std::cout << *(p + 2) << std::endl;
    return 0;
}
ACompilation error
B2
C4
D3
Attempts:
2 left
💡 Hint
Pointer arithmetic moves the pointer by elements, not bytes.
❓ Predict Output
advanced
2:00remaining
Output of double pointer dereferencing
What does this C++ code print?
C++
#include <iostream>
int main() {
    int val = 7;
    int* p1 = &val;
    int** p2 = &p1;
    std::cout << **p2 << std::endl;
    return 0;
}
ACompilation error
BAddress of p1
C7
DAddress of val
Attempts:
2 left
💡 Hint
Double pointer points to a pointer, dereferencing twice gets the value.
❓ Predict Output
expert
3:00remaining
Pointer and reference combined output
What is the output of this C++ code?
C++
#include <iostream>
int main() {
    int x = 3;
    int& ref = x;
    int* ptr = &ref;
    *ptr = 10;
    std::cout << x << std::endl;
    return 0;
}
A10
B3
CCompilation error
DUndefined behavior
Attempts:
2 left
💡 Hint
References are aliases; pointer to reference is pointer to original variable.

Practice

(1/5)
1. What does the & operator do in C++?
easy
A. It dereferences a pointer to get the value.
B. It multiplies two numbers.
C. It declares a pointer variable.
D. It gives the memory address of a 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, declare pointers, or dereference pointers; those are different operations.
  3. Final Answer:

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

    & operator = address retrieval [OK]
Hint: Remember & means 'address of' in C++ [OK]
Common Mistakes:
  • Confusing & with multiplication operator
  • Thinking & declares a pointer
  • Mixing & with dereference * operator
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; declares a reference, not a pointer. int ptr*; and *int ptr; are invalid syntax.
  3. Final Answer:

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

    Pointer declaration = int *ptr; [OK]
Hint: Pointer syntax: type *name; [OK]
Common Mistakes:
  • Using & instead of * for pointers
  • Placing * after variable name incorrectly
  • Confusing pointers with references
3. What is the output of the following code?
int x = 10;
int *p = &x;
*p = 20;
std::cout << x << std::endl;
medium
A. Address of x
B. 10
C. 20
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.
  2. Step 2: Check the effect on x

    Since *p points to x, changing *p to 20 updates x to 20.
  3. Final Answer:

    20 -> Option C
  4. Quick Check:

    Dereferencing pointer changes original variable [OK]
Hint: Changing *p changes the value of x [OK]
Common Mistakes:
  • Thinking *p is the address, not the value
  • Expecting output to be original x (10)
  • Confusing pointer variable with pointed value
4. Identify the error in this code snippet:
int x = 5;
int *p;
*p = x;
std::cout << *p << std::endl;
medium
A. Variable x is not declared.
B. Pointer p is not initialized before dereferencing.
C. Syntax error in pointer declaration.
D. Cannot assign value to *p.

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 initialize pointers before dereferencing [OK]
Hint: Initialize pointers before using *p [OK]
Common Mistakes:
  • Dereferencing pointer without assigning address
  • Assuming pointer auto-points to variable
  • Confusing pointer declaration with initialization
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. 4 3
B. 3 4
C. 3 3
D. 4 4

Solution

  1. Step 1: Understand pointer swapping

    Initially, p points to a (3), q points to b (4). The code swaps the pointers using temp.
  2. Step 2: Check final pointer values

    After swapping, p points to b (4), and q points to a (3).
  3. Final Answer:

    4 3 -> Option A
  4. Quick Check:

    Pointer swap changes what they point to [OK]
Hint: Swapping pointers swaps the values they point to [OK]
Common Mistakes:
  • Confusing swapping pointers with swapping values
  • Expecting original order after swap
  • Mixing pointer values and pointed values
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