Practice ยท 2 of 4
The Vec Push Loop
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 */
``
Difficulty: intermediate
Press Submit to check your solution.
Back to lesson: Practice: Failure-Path Gym