pointers-memory · intermediate · ~15 min

Deep-copy an int array

Allocate + copy. The motivating concept of "deep" vs "shallow" copy.

Challenge

Implement int *copy_ints(const int *src, size_t n) returning a freshly allocated array with the same contents. If n == 0 you may return NULL.

Starter code

#include <stdlib.h>
#include <string.h>
#include <stddef.h>

int *copy_ints(const int *src, size_t n) {
    /* TODO */
    return NULL;
}

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