Skip to main content
C++ Output Prediction Hard +35 XP

Pointer Arithmetic

What is printed?

C++Read-only
int arr[] = {10, 20, 30, 40};
int* ptr = arr;
ptr += 2;
cout << *ptr;

What is the output?

Show answer and explanation

Answer: 30

ptr starts at arr[0]. ptr += 2 moves it to arr[2], which is 30. *ptr dereferences to get the value.

Hint: Pointer arithmetic moves by element size.

What this challenge tests

Output Prediction in C++: pointers, arrays.

Related challenges