pointers-memory · intermediate · ~15 min
Allocate, initialise, and return heap memory.
Allocate a heap array, fill it with a counting sequence, and return it for the caller to use and free.
Implement int *make_range(int n) that mallocs an array of n ints, fills it with 0, 1, ..., n-1, and returns the pointer. No main — the grader calls it and frees the result.
n — the number of ints to allocate (at least 1 in the tests).
Returns a malloc-ed int* of length n holding 0..n-1. The caller owns and frees it.
make_range(5) -> {0, 1, 2, 3, 4}
make_range(1) -> {0}
free.n — the number of ints to allocate and fill.
A malloc-ed int array of length n holding 0..n-1 (caller frees).
Allocate with malloc; the caller frees the result.
#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.