0
0
Cprogramming~20 mins

Common pointer errors - Practice Problems & Coding Challenges

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