Bird
Raised Fist0
Cprogramming~20 mins

Common pointer errors - Practice Problems & Coding Challenges

Choose your learning style10 modes available
Challenge - 5 Problems
🎖️
Pointer Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate
2:00remaining
Output of pointer arithmetic

What is the output of this C code snippet?

C
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30};
    int *p = arr;
    p++;
    printf("%d\n", *p);
    return 0;
}
A30
B20
C10
DCompilation error
Attempts:
2 left
💡 Hint

Remember that incrementing a pointer moves it to the next element in the array.

❓ Predict Output
intermediate
2:00remaining
Dereferencing a NULL pointer

What happens when this code runs?

C
#include <stdio.h>

int main() {
    int *p = NULL;
    printf("%d\n", *p);
    return 0;
}
ARuntime error (segmentation fault)
BPrints garbage value
CPrints 0
DCompilation error
Attempts:
2 left
💡 Hint

Think about what happens when you try to access memory through a NULL pointer.

🔧 Debug
advanced
2:00remaining
Identify the pointer error

What is the error in this code?

C
#include <stdio.h>

int main() {
    int *p;
    *p = 5;
    printf("%d\n", *p);
    return 0;
}
AIncorrect format specifier in <code>printf</code>
BMissing <code>return</code> statement
CPointer <code>p</code> is uninitialized and used to write memory
DArray index out of bounds
Attempts:
2 left
💡 Hint

Think about what p points to before assignment.

📝 Syntax
advanced
2:00remaining
Syntax error with pointer declaration

Which option contains the correct pointer declaration?

Aint *p;
Bint * p[];
Cint p*;
Dint &p;
Attempts:
2 left
💡 Hint

Remember how to declare a pointer to an int in C.

🚀 Application
expert
2:00remaining
Pointer and memory allocation behavior

What is the output of this program?

C
#include <stdio.h>
#include <stdlib.h>

int main() {
    int *p = malloc(sizeof(int));
    *p = 42;
    free(p);
    printf("%d\n", *p);
    return 0;
}
A42
B0
CCompilation error
DRuntime error (use after free)
Attempts:
2 left
💡 Hint

Consider what happens when you access memory after it has been freed.

Practice

(1/5)
1. What is a common mistake when using pointers in C?
easy
A. Assigning a valid memory address to a pointer
B. Declaring pointers with the correct syntax
C. Using uninitialized pointers before assigning them an address
D. Using pointers to access array elements correctly

Solution

  1. Step 1: Understand pointer initialization

    Pointers must be assigned a valid memory address before use to avoid undefined behavior.
  2. Step 2: Identify common mistakes

    Using a pointer without initialization can cause crashes or unexpected results.
  3. Final Answer:

    Using uninitialized pointers before assigning them an address -> Option C
  4. Quick Check:

    Uninitialized pointer use = common error [OK]
Hint: Always initialize pointers before use to avoid errors [OK]
Common Mistakes:
  • Dereferencing NULL pointers
  • Forgetting to initialize pointers
  • Confusing pointer and value assignment
2. Which of the following pointer declarations is syntactically correct in C?
easy
A. *int ptr;
B. int ptr*;
C. int &ptr;
D. int *ptr;

Solution

  1. Step 1: Review pointer declaration syntax

    In C, the asterisk (*) goes between the type and the pointer name to declare a pointer.
  2. Step 2: Check each option

    int *ptr; correctly declares a pointer to int. Options A, B, and D have invalid syntax.
  3. Final Answer:

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

    Correct pointer syntax = int *ptr; [OK]
Hint: Pointer syntax: type *name; always asterisk before name [OK]
Common Mistakes:
  • Placing * after variable name
  • Using & instead of * for pointers
  • Incorrect order of type and *
3. What will be the output of the following C code?
int x = 10;
int *p = &x;
*p = 20;
printf("%d", x);
medium
A. 10
B. 20
C. Garbage value
D. Compilation error

Solution

  1. Step 1: Understand pointer assignment

    Pointer p points to variable x. Changing *p changes x's value.
  2. Step 2: Trace the code execution

    *p = 20 sets x to 20. printf prints x, so output is 20.
  3. Final Answer:

    20 -> Option B
  4. Quick Check:

    Pointer dereference changes value = 20 [OK]
Hint: Changing *p changes the value pointed to [OK]
Common Mistakes:
  • Confusing pointer address and value
  • Expecting original value unchanged
  • Ignoring pointer dereference effect
4. Identify the error in the following code snippet:
int *p;
*p = 5;
printf("%d", *p);
medium
A. Pointer p is not initialized before dereferencing
B. Syntax error in pointer declaration
C. Incorrect printf format specifier
D. No error, code runs fine

Solution

  1. Step 1: Check pointer initialization

    Pointer p is declared but not assigned an address before use.
  2. Step 2: Understand dereferencing consequences

    Dereferencing uninitialized pointer causes undefined behavior or crash.
  3. Final Answer:

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

    Uninitialized pointer dereference = error [OK]
Hint: Always assign pointer before dereferencing [OK]
Common Mistakes:
  • Assuming declaration initializes pointer
  • Ignoring runtime crashes from bad pointers
  • Misreading error as printf issue
5. Consider the code below. What is the safest way to avoid a common pointer error here?
int *p = NULL;
// Some code
printf("%d", *p);
hard
A. Check if p is NULL before dereferencing
B. Assign p to a random memory address
C. Remove the pointer and use an int variable instead
D. Ignore the NULL and dereference directly

Solution

  1. Step 1: Understand NULL pointer risks

    Dereferencing NULL pointer causes runtime crash or undefined behavior.
  2. Step 2: Apply safe pointer usage

    Always check if pointer is NULL before dereferencing to avoid errors.
  3. Final Answer:

    Check if p is NULL before dereferencing -> Option A
  4. Quick Check:

    NULL check before dereference = safe code [OK]
Hint: Always test pointer != NULL before use [OK]
Common Mistakes:
  • Dereferencing NULL pointer causing crash
  • Assigning invalid addresses
  • Ignoring pointer safety checks
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