Bird
Raised Fist0
C++programming~20 mins

Pointer declaration 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
What is the output of this pointer declaration code?
Consider the following C++ code snippet. What will be printed when it runs?
C++
#include <iostream>
int main() {
    int x = 10;
    int* p = &x;
    std::cout << *p << std::endl;
    return 0;
}
AAddress of x
B0
CCompilation error
D10
Attempts:
2 left
💡 Hint
Remember that *p means the value pointed to by p.
❓ Predict Output
intermediate
2:00remaining
What is the value of variable 'p' after this declaration?
Look at this code snippet. What does the pointer 'p' hold immediately after declaration?
C++
int* p;
AIt holds the address of some variable
BIt holds nullptr
CIt holds a garbage (undefined) value
DIt holds 0
Attempts:
2 left
💡 Hint
Uninitialized pointers do not automatically point to nullptr.
❓ Predict Output
advanced
2:00remaining
What error does this pointer declaration cause?
What error will this code produce?
C++
int* p = 5;
ACompilation error: cannot convert int to int*
BRuntime error: segmentation fault
CWarning but compiles
DNo error, p points to address 5
Attempts:
2 left
💡 Hint
You cannot assign an integer directly to a pointer without casting.
❓ Predict Output
advanced
2:00remaining
What is the output of this pointer declaration and dereference?
What will this program print?
C++
#include <iostream>
int main() {
    int x = 7;
    int* p = &x;
    *p = 20;
    std::cout << x << std::endl;
    return 0;
}
A20
BGarbage value
CCompilation error
D7
Attempts:
2 left
💡 Hint
Changing *p changes the value of x because p points to x.
🧠 Conceptual
expert
3:00remaining
Which pointer declaration is correct for a pointer to a constant integer?
You want to declare a pointer that points to an integer value that cannot be changed through the pointer. Which declaration is correct?
Aint* const p;
Bconst int* p;
Cconst int* const p;
Dint const p*;
Attempts:
2 left
💡 Hint
The const keyword before int means the value pointed to is constant.

Practice

(1/5)
1. Which of the following correctly declares a pointer to an integer in C++?
easy
A. ptr int*;
B. int ptr*;
C. *int ptr;
D. int *ptr;

Solution

  1. Step 1: Understand pointer declaration syntax

    In C++, a pointer to a type is declared by writing the type followed by an asterisk (*) and then the pointer name.
  2. Step 2: Check each option

    int *ptr; uses int *ptr; which is the correct syntax. Other options place the asterisk incorrectly.
  3. Final Answer:

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

    Pointer declaration = type *name [OK]
Hint: Remember: type *name; declares a pointer [OK]
Common Mistakes:
  • Placing * after the variable name
  • Using * before the type keyword
  • Omitting the * entirely
2. Which of the following is the correct syntax to declare a pointer to a double variable named p?
easy
A. double *p;
B. p double*;
C. *double p;
D. double p*;

Solution

  1. Step 1: Recall pointer declaration rules

    The pointer declaration syntax is type *pointerName;. The asterisk (*) must come immediately before the pointer name.
  2. Step 2: Evaluate options

    double *p; correctly declares double *p;. Other options have misplaced asterisks or wrong order.
  3. Final Answer:

    double *p; -> Option A
  4. Quick Check:

    Pointer to double = double *p; [OK]
Hint: Put * right before pointer name, after type [OK]
Common Mistakes:
  • Writing * after the variable name
  • Placing * before the type keyword
  • Swapping variable and type order
3. What is the output of the following code?
int x = 10;
int *p = &x;
std::cout << *p << std::endl;
medium
A. Garbage value
B. Address of x
C. 10
D. Compilation error

Solution

  1. Step 1: Understand pointer initialization

    The pointer p is assigned the address of x using &x. So p points to x.
  2. Step 2: Dereference pointer to get value

    The expression *p accesses the value stored at the address p points to, which is 10.
  3. Final Answer:

    10 -> Option C
  4. Quick Check:

    *p gives value at address = 10 [OK]
Hint: Dereference pointer *p to get variable value [OK]
Common Mistakes:
  • Printing pointer variable instead of dereferenced value
  • Confusing & (address) with * (dereference)
  • Expecting address output instead of value
4. Identify the error in the following code snippet:
int a = 5;
int *ptr;
*ptr = a;
medium
A. Pointer ptr is not initialized before dereferencing
B. Variable a is not declared
C. Incorrect pointer declaration syntax
D. Missing semicolon after declaration

Solution

  1. Step 1: Check pointer initialization

    The pointer ptr is declared but not assigned any address before dereferencing.
  2. Step 2: Understand dereferencing uninitialized pointer

    Dereferencing *ptr without initialization leads to undefined behavior or runtime error.
  3. Final Answer:

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

    Uninitialized pointer dereference = error [OK]
Hint: Always initialize pointer before using *ptr [OK]
Common Mistakes:
  • Dereferencing pointer before assigning address
  • Assuming declaration initializes pointer
  • Ignoring runtime errors from uninitialized pointers
5. Given the code below, which pointer declaration correctly allows p to point to a constant integer value?
const int val = 100;
// Which declaration is correct?
hard
A. int *p = &val;
B. const int *p = &val;
C. int *const p = &val;
D. int *p const = &val;

Solution

  1. Step 1: Understand pointer to const int

    To point to a constant integer, the pointer must be declared as pointing to a const int to prevent modification.
  2. Step 2: Analyze options

    int *p = &val; cannot assign const int* to int* (discards qualifiers). const int *p = &val; correctly declares pointer to const int. int *const p = &val; declares const pointer to int (type mismatch). int *p const = &val; invalid syntax.
  3. Final Answer:

    const int *p = &val; -> Option B
  4. Quick Check:

    Pointer to const int = const int *p [OK]
Hint: Use const int *p to point to constant int [OK]
Common Mistakes:
  • Using int *p to point to const int
  • Confusing const pointer and pointer to const
  • Misplacing const keyword
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