0
0
Cprogramming~20 mins

Pointer arithmetic - 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 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.