Bird
Raised Fist0
Cprogramming~20 mins

Pointer arithmetic - 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 increment on array
What is the output of this C code snippet?
C
#include <stdio.h>

int main() {
    int arr[] = {10, 20, 30, 40};
    int *p = arr;
    p++;
    printf("%d\n", *p);
    return 0;
}
A20
B10
C30
D40
Attempts:
2 left
💡 Hint
Incrementing a pointer moves it to the next element in the array.
❓ Predict Output
intermediate
2:00remaining
Pointer subtraction result
What is the output of this C code?
C
#include <stdio.h>

int main() {
    int arr[] = {5, 10, 15, 20, 25};
    int *p1 = &arr[4];
    int *p2 = &arr[1];
    printf("%td\n", p1 - p2);
    return 0;
}
A3
B6
C5
D4
Attempts:
2 left
💡 Hint
Pointer subtraction gives the number of elements between them.
🔧 Debug
advanced
2:00remaining
Identify the error in pointer arithmetic
What error will this C code produce when compiled or run?
C
#include <stdio.h>

int main() {
    int x = 10;
    int *p = &x;
    p = p + 2;
    printf("%d\n", *p);
    return 0;
}
ACompilation error: invalid pointer arithmetic
BOutput: 10
CRuntime error: segmentation fault or undefined behavior
DOutput: 0
Attempts:
2 left
💡 Hint
Pointer arithmetic beyond allocated memory is unsafe.
📝 Syntax
advanced
2:00remaining
Which pointer arithmetic expression is valid?
Which of the following pointer arithmetic expressions is valid in C?
Ap / 2
Bp - p
Cp * 2
Dp + p
Attempts:
2 left
💡 Hint
Pointer subtraction between two pointers to the same array is allowed.
🚀 Application
expert
3:00remaining
Calculate sum using pointer arithmetic
What is the output of this C program that sums array elements using pointer arithmetic?
C
#include <stdio.h>

int main() {
    int arr[] = {1, 2, 3, 4, 5};
    int *p = arr;
    int *end = arr + 5;
    int sum = 0;
    while (p < end) {
        sum += *p++;
    }
    printf("%d\n", sum);
    return 0;
}
A0
B10
C5
D15
Attempts:
2 left
💡 Hint
Pointer p moves from start to end of array, adding each element.

Practice

(1/5)
1. What does adding 1 to a pointer in C do?
int *p;
easy
A. Causes a syntax error
B. Moves the pointer by 1 byte
C. Moves the pointer to the previous element
D. Moves the pointer to the next element of the type it points to

Solution

  1. Step 1: Understand pointer arithmetic basics

    In C, 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 the given pointer

    Since p is an int*, p + 1 points to the next int in memory.
  3. Final Answer:

    Moves the pointer to the next element of the type it points to -> Option D
  4. Quick Check:

    Pointer + 1 moves by element size [OK]
Hint: Pointer + 1 moves to next element, not next byte [OK]
Common Mistakes:
  • Thinking pointer arithmetic moves by 1 byte
  • Confusing pointer increment with value increment
  • Assuming pointer addition causes errors
2. Which of the following pointer arithmetic expressions is syntactically correct in C?
int arr[5];
int *p = arr;
easy
A. p + 2
B. p ++ 2
C. p + ++ 2
D. p +* 2

Solution

  1. Step 1: Review pointer arithmetic syntax

    Valid pointer arithmetic uses p + n or p - n where n is an integer.
  2. Step 2: Check each option

    p + 2 is valid syntax. Options A, C, and D contain invalid operators or spacing causing syntax errors.
  3. Final Answer:

    p + 2 -> Option A
  4. Quick Check:

    Correct pointer addition syntax is p + n [OK]
Hint: Pointer arithmetic uses '+' or '-' with integers only [OK]
Common Mistakes:
  • Using invalid operators like '++' incorrectly
  • Adding spaces that change meaning
  • Confusing pointer increment with addition
3. What is the output of this code?
int arr[] = {10, 20, 30, 40};
int *p = arr;
printf("%d", *(p + 2));
medium
A. 10
B. 20
C. 30
D. 40

Solution

  1. Step 1: Understand pointer and array relation

    Pointer p points to arr[0], which is 10.
  2. Step 2: Calculate pointer offset

    *(p + 2) accesses the element 2 positions after arr[0], which is arr[2] = 30.
  3. Final Answer:

    30 -> Option C
  4. Quick Check:

    Pointer + 2 points to third element = 30 [OK]
Hint: *(p + n) accesses nth element after pointer [OK]
Common Mistakes:
  • Confusing index starting at 1 instead of 0
  • Dereferencing pointer without offset
  • Mixing pointer arithmetic with array indexing
4. Identify the error in this code snippet:
int arr[3] = {1, 2, 3};
int *p = arr + 3;
printf("%d", *p);
medium
A. Syntax error in pointer declaration
B. Pointer points beyond array bounds causing undefined behavior
C. Missing semicolon after array initialization
D. Incorrect printf format specifier

Solution

  1. Step 1: Analyze pointer assignment

    p = arr + 3 points to one element past the last valid element (index 3 in a 3-element array).
  2. Step 2: Understand dereferencing invalid pointer

    Dereferencing *p here causes undefined behavior because it accesses memory outside the array.
  3. Final Answer:

    Pointer points beyond array bounds causing undefined behavior -> Option B
  4. Quick Check:

    Pointer must not dereference beyond array end [OK]
Hint: Pointer + array size points just past array, not valid to dereference [OK]
Common Mistakes:
  • Assuming pointer can safely dereference past array end
  • Confusing pointer arithmetic with array length
  • Ignoring undefined behavior risks
5. Given two pointers int *p1 and int *p2 pointing inside the same array, what does p2 - p1 represent?
hard
A. The number of elements between the two pointers
B. The difference in memory addresses as a raw number
C. The number of bytes between the two pointers
D. A syntax error because pointers cannot be subtracted

Solution

  1. Step 1: Understand pointer subtraction in C

    Subtracting two pointers gives the number of elements of the pointer type between them, not bytes.
  2. Step 2: Apply to pointers inside the same array

    Since p1 and p2 point inside the same array, p2 - p1 counts how many elements separate them.
  3. Final Answer:

    The number of elements between the two pointers -> Option A
  4. Quick Check:

    Pointer subtraction = element count difference [OK]
Hint: Pointer subtraction counts elements, not bytes [OK]
Common Mistakes:
  • Thinking subtraction returns byte difference
  • Assuming pointer subtraction is invalid
  • Confusing pointer subtraction with pointer addition
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