pointers-memory · beginner · ~15 min
Iterate an array by moving a pointer.
Sum the elements of an array by walking a pointer across it rather than indexing.
Implement long sum_via_ptr(const int *a, int n) that returns the sum of the first n elements, advancing a pointer from a to a + n. No main — the grader calls it.
a — pointer to an int array. n — the element count (may be 0).
Returns the sum of the n elements as a long.
int a[] = {1,2,3,4,5};
sum_via_ptr(a, 5) -> 15
sum_via_ptr(a, 0) -> 0
n == 0: returns 0.a — pointer to an int array; n — element count (may be 0).
The sum of the n elements, as a long.
Iterate by advancing a pointer, not by indexing.
long sum_via_ptr(const int *a, int n) {
/* TODO */
return 0;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.