pointers-memory · intermediate · ~15 min

Capacity-checked copy

Bound a copy by the destination capacity.

Challenge

Implement int safe_copy_n(int *dst, int dstcap, const int *src, int n) copying at most min(n, dstcap) ints into dst and returning the number actually copied — never overrunning dst.

Starter code

int safe_copy_n(int *dst, int dstcap, const int *src, int n) {
    /* TODO */
    return 0;
}

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