Simvik

Sum a pointer range

Walk a half-open range with a pointer. An easy embedded C problem (Pointers), run and graded in the browser.

easy · Pointers · pointer-arithmetic

Return the sum of the elements from begin up to, but not including, end.

end points one past the last element. It may be compared with other pointers but must never be dereferenced.

Starter code

int32_t sum_range(const int32_t *begin, const int32_t *end) {
  // Add up every element in [begin, end).
  return 0;
}

Example tests

Whole array

int32_t d[] = { 1, 2, 3, 4 };
printf("%d\n", sum_range(d, d + 4));

Expected output:

10

Middle of an array

int32_t d[] = { 1, 2, 3, 4 };
printf("%d\n", sum_range(d + 1, d + 3));

Expected output:

5

Learn it first

  • Pointers · Addresses, arithmetic, const, void pointers and object lifetime.

More Pointers problems