pointers-memory · intermediate · ~15 min
Use calloc for zeroed memory.
Allocate a zero-initialised int array using calloc, which both reserves and zeroes the memory in one call.
Implement int *zeros(int n) that returns an array of n ints allocated with calloc, so every element is 0. No main — the grader calls it and frees the result.
n — the number of ints to allocate.
Returns a calloc-ed int* of length n, all elements 0. The caller frees it.
zeros(4) -> {0, 0, 0, 0}
calloc (not malloc + manual zeroing). The caller frees the result.n — the number of ints to allocate.
A calloc-ed int array of length n, all zero (caller frees).
Use calloc to allocate and zero in one step; the caller frees it.
#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.