Skip to main content

Practice · 2 of 4

The Vec Push Loop

CHALLENGE
Difficulty: intermediate+25 XP

Implement a growable int vector with the exact realloc discipline: ``c typedef struct { int *data; size_t len, cap; } Vec; /* in boilerplate */ void vec_init(Vec *v); int vec_push(Vec *v, int value); /* 0 on allocation failure; v unchanged then */ size_t vec_len(const Vec *v); int vec_get(const Vec *v, size_t i, int *out); /* bounds-checked; 0 if out of range */ void vec_destroy(Vec *v); /* frees and zeroes the struct */ ``

Back to lesson: Practice: Failure-Path Gym