Bird
Raised Fist0
C++programming~20 mins

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

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

Practice

(1/5)
1. What does adding 1 to a pointer in C++ do when the pointer points to an int array?
easy
A. Moves the pointer to the next integer element in the array
B. Increases the pointer's memory address by 1 byte
C. Moves the pointer to the next byte in memory
D. Causes a compilation error

Solution

  1. Step 1: Understand pointer arithmetic basics

    Adding 1 to a pointer moves it to the next element of the type it points to, not just 1 byte.
  2. Step 2: Apply to int pointer

    Since int usually occupies 4 bytes, adding 1 moves the pointer by 4 bytes to the next integer.
  3. Final Answer:

    Moves the pointer to the next integer element in the array -> Option A
  4. Quick Check:

    Pointer + 1 = next element [OK]
Hint: Pointer + 1 moves to next element, not next byte [OK]
Common Mistakes:
  • Thinking pointer arithmetic adds 1 byte always
  • Confusing pointer increment with byte increment
  • Assuming pointer arithmetic causes errors
2. Which of the following pointer arithmetic expressions is syntactically correct in C++?
easy
A. 1 + ptr
B. All of the above
C. ptr - 1
D. ptr + 1

Solution

  1. Step 1: Check pointer addition syntax

    Adding an integer to a pointer like ptr + 1 is valid and moves the pointer forward.
  2. Step 2: Check commutativity and subtraction

    Integer + pointer like 1 + ptr is also valid due to commutativity. Pointer - integer like ptr - 1 moves pointer backward and is valid.
  3. Final Answer:

    All of the above -> Option B
  4. Quick Check:

    Pointer + int and int + pointer both valid [OK]
Hint: Pointer + int and int + pointer both work [OK]
Common Mistakes:
  • Assuming int + pointer is invalid
  • Thinking pointer - int is not allowed
  • Confusing pointer arithmetic with pointer subtraction
3. What is the output of the following C++ code?
int arr[] = {10, 20, 30, 40};
int *p = arr;
int *q = arr + 3;
std::cout << q - p << std::endl;
medium
A. 3
B. 4
C. 30
D. Compilation error

Solution

  1. Step 1: Understand pointer subtraction

    Subtracting two pointers gives the number of elements between them, not bytes.
  2. Step 2: Calculate difference between pointers

    q = arr + 3 points to the 4th element, p = arr points to the 1st. Difference is 3 elements.
  3. Final Answer:

    3 -> Option A
  4. Quick Check:

    Pointer difference = number of elements [OK]
Hint: Pointer subtraction returns element count, not bytes [OK]
Common Mistakes:
  • Thinking pointer subtraction returns byte difference
  • Confusing pointer difference with array values
  • Expecting compilation error
4. Identify the error in the following code snippet:
int arr[5] = {1, 2, 3, 4, 5};
int *p = arr + 5;
std::cout << *p << std::endl;
medium
A. Array size is incorrect
B. Syntax error in pointer initialization
C. Pointer points outside array bounds, causing undefined behavior
D. Dereferencing pointer is valid and prints 5

Solution

  1. Step 1: Analyze pointer initialization

    p = arr + 5 points one past the last element (index 5), which is outside the array bounds.
  2. Step 2: Check dereferencing pointer

    Dereferencing *p here is undefined behavior because pointer is out of valid range.
  3. Final Answer:

    Pointer points outside array bounds, causing undefined behavior -> Option C
  4. Quick Check:

    Dereferencing out-of-bounds pointer is unsafe [OK]
Hint: Pointer must not dereference beyond array end [OK]
Common Mistakes:
  • Assuming pointer at arr+5 is valid to dereference
  • Confusing pointer arithmetic with array indexing
  • Ignoring undefined behavior risks
5. Given the code below, what will be the output?
int arr[] = {5, 10, 15, 20, 25};
int *p = arr + 1;
int *q = arr + 4;
int diff = q - p;
std::cout << *(p + diff) << std::endl;
hard
A. Undefined behavior
B. 20
C. Compilation error
D. 25

Solution

  1. Step 1: Calculate pointer difference

    q - p = (arr + 4) - (arr + 1) = 3 elements difference.
  2. Step 2: Evaluate pointer arithmetic and dereference

    p + diff = (arr + 1) + 3 = arr + 4, which points to the element 25. Dereferencing gives 25.
  3. Final Answer:

    25 -> Option D
  4. Quick Check:

    Pointer arithmetic + dereference = correct element [OK]
Hint: Pointer difference + pointer = new pointer position [OK]
Common Mistakes:
  • Confusing pointer difference with value difference
  • Miscalculating pointer arithmetic
  • Assuming undefined behavior without reason
Recommended

Ready to prove it?

Pass the test and this pattern moves unknown strong while earning +1 Grip toward your certificate.

10 questions~5 mineasy hard