pointers-memory · intermediate · ~15 min

Zero-initialised allocation

Use calloc for zeroed memory.

Challenge

Implement int *zeros(int n) returning an array of n ints allocated with calloc (so every element is 0). The caller frees it.

Starter code

#include <stdlib.h>

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

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