pointers-memory · intermediate · ~10 min
Use pointer increment and two-pointer ranges (like C++ iterators).
Sum a slice of an int array described by a begin/end pointer pair (like C++ iterators).
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.
Two pointers into the same array: begin (inclusive) and end (exclusive). The range may be empty (begin == end).
The sum of the elements from begin up to but not including end, as a long.
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
begin == end: the range is empty, return 0.Transfer exercise: apply the lesson to a complete function contract.
Two pointers into the same array: begin (inclusive) and end (exclusive). The range may be empty (begin == end).
The sum of the elements from begin up to but not including end, as a long.
Live same-array begin/end with begin <= end, including one-past end; each partial sum fits long. Read only.
#include <stddef.h>
long sum_walk(const int *begin, const int *end) {
/* TODO */
return 0;
}
Reading end or treating the range as inclusive.
Empty half-open range; subrange; mixed signs; exclude the end element.
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.