pointers-memory · intermediate · ~15 min

Append with realloc

Grow a heap buffer and use realloc's returned pointer.

Challenge

Implement int *append(int *a, int n, int v) that grows the n-element heap array a to n+1 with realloc, stores v at the new last slot, and returns the (possibly moved) pointer.

Starter code

#include <stdlib.h>

int *append(int *a, int n, int v) {
    /* TODO */
    return a;
}

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