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 <iostream> int main() { int arr[] = {10, 20, 30, 40}; int *p = arr; std::cout << *(p + 2) << std::endl; 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, which is 30.
❓ Predict Output
intermediate2:00remaining
Value of pointer after increment
What will be the value printed by this program?
C++
#include <iostream> int main() { int arr[3] = {5, 10, 15}; int *ptr = arr + 1; std::cout << *ptr << std::endl; ptr++; std::cout << *ptr << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Pointer starts at second element, then moves to third.
✗ Incorrect
ptr initially points to arr[1] which is 10, then after ptr++ it points to arr[2] which is 15.
🔧 Debug
advanced2:00remaining
Identify the error in pointer usage
What error will this code produce when compiled or run?
C++
#include <iostream> int main() { int *p; *p = 10; std::cout << *p << std::endl; return 0; }
Attempts:
2 left
💡 Hint
Think about 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. Dereferencing it causes a runtime segmentation fault.
🧠 Conceptual
advanced2:00remaining
Size of array vs pointer
Given the code below, what will be the output?
C++
#include <iostream> int main() { int arr[5] = {1,2,3,4,5}; int *p = arr; std::cout << sizeof(arr) << " " << sizeof(p) << std::endl; return 0; }
Attempts:
2 left
💡 Hint
sizeof(arr) gives total bytes of all elements, sizeof(p) gives size of pointer variable.
✗ Incorrect
arr is an array of 5 ints, each 4 bytes, so 20 bytes total. p is a pointer, typically 8 bytes on 64-bit systems.
📝 Syntax
expert2:00remaining
Correct pointer declaration and initialization
Which option correctly declares and initializes a pointer to the first element of an integer array named 'data'?
Attempts:
2 left
💡 Hint
Pointer must be declared with * and initialized with address of an element.
✗ Incorrect
Option C correctly declares a pointer to int and initializes it with the address of the first element of the array.