pointers-memory · intermediate · ~15 min

Sum via pointer walking

Use pointer increment and two-pointer ranges (like C++ iterators).

Challenge

Sum a slice of an int array described by a begin/end pointer pair (like C++ iterators).

Task

Implement long sum_walk(const int *begin, const int *end) that returns the sum of the ints in the half-open range [begin, end). No main — the grader calls it.

Input

Two pointers into the same array: begin (inclusive) and end (exclusive). The range may be empty (begin == end).

Output

The sum of the elements from begin up to but not including end, as a long.

Example

sum_walk(a, a+5)   where a={1,2,3,4,5}   ->   15
sum_walk(a, a)                            ->   0
sum_walk(a+1, a+4) where a={1,2,3,4,5}   ->   9

Edge cases

  • begin == end: the range is empty, return 0.

Rules

  • Walk with pointer arithmetic; do not take a separate length parameter.

Input format

Two pointers begin (inclusive) and end (exclusive) into one int array.

Output format

The sum of the elements in [begin, end) as a long.

Constraints

Use pointer arithmetic, no separate length; empty range returns 0.

Starter code

#include <stddef.h>

long sum_walk(const int *begin, const int *end) {
    /* TODO */
    return 0;
}

Solve this exercise in the browser editor — compile and run against the test harness, no setup required.