Challenge - 5 Problems
Pointer and Array Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
❓ Predict Output
intermediate2:00remaining
Output of pointer arithmetic with arrays
What is the output of this C code snippet?
C
#include <stdio.h> int main() { int arr[] = {10, 20, 30, 40}; int *p = arr; printf("%d\n", *(p + 2)); return 0; }
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves by the size of the data type.
✗ Incorrect
The pointer p points to the first element of arr. Adding 2 moves it to the third element (index 2), which is 30.
❓ Predict Output
intermediate2:00remaining
Value of pointer after increment
What will be the value printed by this code?
C
#include <stdio.h> int main() { char str[] = "hello"; char *p = str; p++; printf("%c\n", *p); return 0; }
Attempts:
2 left
💡 Hint
Incrementing a pointer moves it to the next character.
✗ Incorrect
Initially, p points to 'h'. After p++, it points to 'e', so *p is 'e'.
🔧 Debug
advanced2:00remaining
Identify the error in pointer usage
What error does this code produce when compiled or run?
C
#include <stdio.h> int main() { int *p; *p = 5; printf("%d\n", *p); return 0; }
Attempts:
2 left
💡 Hint
Consider what happens when you dereference a pointer that has not been assigned a valid address.
✗ Incorrect
Pointer p is uninitialized and points to an unknown memory location. Writing to *p causes a segmentation fault at runtime.
❓ Predict Output
advanced2:00remaining
Output of pointer to array and sizeof
What is the output of this code?
C
#include <stdio.h> int main() { int arr[5] = {1,2,3,4,5}; int (*p)[5] = &arr; printf("%zu\n", sizeof(p)); return 0; }
Attempts:
2 left
💡 Hint
sizeof(p) gives the size of the pointer, not the array it points to.
✗ Incorrect
p is a pointer to an array of 5 ints. sizeof(p) returns the size of the pointer itself, typically 8 bytes on 64-bit systems.
🧠 Conceptual
expert2:00remaining
Number of elements accessed via pointer arithmetic
Given the code below, how many elements of the array can be safely accessed using the pointer p?
C
#include <stdio.h> int main() { int arr[4] = {10, 20, 30, 40}; int *p = arr + 1; // How many elements can be accessed safely via p? return 0; }
Attempts:
2 left
💡 Hint
Pointer p points to the second element. Count elements from there to the end.
✗ Incorrect
p points to arr[1]. The array has 4 elements total, so elements at indices 1, 2, and 3 can be accessed safely, totaling 3 elements.