Bird
Raised Fist0
Cprogramming~10 mins

Common pointer errors - Interactive Code Practice

Choose your learning style10 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to declare a pointer to an integer.

C
int [1] ptr;
Drag options to blanks, or click blank then click option'
A*
B&
C->
D%
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare a pointer.
Confusing pointer declaration with address-of operator.
2fill in blank
medium

Complete the code to assign the address of variable x to pointer p.

C
int x = 10;
int *p;
p = [1]x;
Drag options to blanks, or click blank then click option'
A*
B&
C%
D->
Attempts:
3 left
💡 Hint
Common Mistakes
Using * instead of & when assigning address to pointer.
Trying to assign the value instead of the address.
3fill in blank
hard

Fix the error in the code to correctly access the value pointed by p.

C
int x = 5;
int *p = &x;
int y = [1]; 
Drag options to blanks, or click blank then click option'
Ap*
Bp
C&p
D*p
Attempts:
3 left
💡 Hint
Common Mistakes
Using the pointer variable itself instead of dereferencing it.
Using & operator which gives the address, not the value.
4fill in blank
hard

Fill both blanks to create a pointer to an array and access its first element.

C
int arr[3] = {1, 2, 3};
int [1] p = arr;
int first = [2]; 
Drag options to blanks, or click blank then click option'
A*
B&
Cp[0]
D[]
Attempts:
3 left
💡 Hint
Common Mistakes
Using & instead of * to declare pointer.
Trying to dereference pointer incorrectly.
5fill in blank
hard

Fill all three blanks to correctly allocate memory, assign value, and free it.

C
#include <stdlib.h>

int *p = (int*)[1](sizeof(int));
*p = [2];
[3](p);
Drag options to blanks, or click blank then click option'
Amalloc
B42
Cfree
Dsizeof
Attempts:
3 left
💡 Hint
Common Mistakes
Forgetting to cast malloc's return value.
Not freeing allocated memory causing memory leaks.
Assigning value to pointer instead of dereferenced pointer.

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