pointers-memory · intermediate · ~15 min

Concatenate two arrays

Allocate exactly the combined size and copy both inputs.

Challenge

Implement int *concat_arrays(const int *a, int na, const int *b, int nb) returning a new heap array of na+nb ints with a then b. The caller frees it.

Starter code

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

int *concat_arrays(const int *a, int na, const int *b, int nb) {
    /* TODO */
    return NULL;
}

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