Skip to main content

Practice · 5 of 5

Sum Through a Callback

CHALLENGE
Difficulty: advanced+25 XP

Implement void foreach_int(const int *a, size_t n, void (*fn)(int v, void *ctx), void *ctx) calling fn once per element, and use it to implement long long sum_with_ctx(const int *a, size_t n) which totals the array through a stack-allocated context struct — no globals. The ctx pattern is the point.

Back to lesson: Practice: Pointer Semantics Drills