0
0
C++programming~20 mins

Pointer arithmetic in C++ - Practice Problems & Coding Challenges

Choose your learning style9 modes available
Challenge - 5 Problems
🎖️
Pointer Arithmetic Master
Get all challenges correct to earn this badge!
Test your skills under time pressure!
Predict Output
intermediate
2:00remaining
Output of pointer arithmetic with arrays
What is the output of the following C++ code?
C++
#include <iostream>
int main() {
    int arr[] = {10, 20, 30, 40};
    int* p = arr;
    std::cout << *(p + 2) << std::endl;
    return 0;
}
A10
B30
C20
D40
Attempts:
2 left
💡 Hint
Remember that pointer arithmetic moves by the size of the data type.
Predict Output
intermediate
2:00remaining
Pointer subtraction result
What is the output of this C++ code snippet?
C++
#include <iostream>
int main() {
    int arr[] = {5, 10, 15, 20, 25};
    int* p1 = &arr[4];
    int* p2 = &arr[1];
    std::cout << p1 - p2 << std::endl;
    return 0;
}
A6
B4
C5
D3
Attempts:
2 left
💡 Hint
Pointer subtraction gives the number of elements between two pointers.
Predict Output
advanced
2:00remaining
Pointer arithmetic with char arrays
What does this program print?
C++
#include <iostream>
int main() {
    char str[] = "hello";
    char* p = str;
    std::cout << *(p + 4) << std::endl;
    return 0;
}
Ao
Bh
Ce
Dl
Attempts:
2 left
💡 Hint
Pointer arithmetic on char arrays moves one character at a time.
Predict Output
advanced
2:00remaining
Pointer arithmetic with void pointers
What error does this code produce?
C++
#include <iostream>
int main() {
    int x = 10;
    void* p = &x;
    p = p + 1;
    return 0;
}
ACompilation error: invalid operands to binary expression
BRuntime error: segmentation fault
CNo error, compiles and runs fine
DCompilation error: void pointer cannot be assigned
Attempts:
2 left
💡 Hint
Pointer arithmetic requires knowing the size of the pointed type.
🧠 Conceptual
expert
2:00remaining
Pointer arithmetic and array indexing equivalence
Which option correctly explains why *(arr + i) is equivalent to arr[i] in C++?
ABecause arr[i] is a macro that expands to *(arr + i).
BBecause arr[i] uses pointer subtraction internally to access elements.
CBecause arr[i] is defined as *(arr + i) by the language standard.
DBecause arr[i] is a function call that returns *(arr + i).
Attempts:
2 left
💡 Hint
Think about how array indexing is defined in terms of pointers.