pointers-memory · intermediate · ~15 min

Allocate a range

Allocate, initialise, and return heap memory.

Challenge

Implement int *make_range(int n) that allocates an array of n ints with malloc, fills it with 0,1,...,n-1, and returns it. The caller frees it.

Starter code

#include <stdlib.h>

int *make_range(int n) {
    /* TODO */
    return NULL;
}

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