pointers-memory · intermediate · ~15 min
Shrink a buffer while keeping its prefix.
Implement int *shrink(int *a, int oldn, int newn) that shrinks the heap array to newn elements (newn <= oldn) with realloc, preserving the first newn values, and returns the pointer.
#include <stdlib.h>
int *shrink(int *a, int oldn, int newn) {
/* TODO */
return a;
}
Solve this exercise in the browser editor — compile and run against the test harness, no setup required.