c - How to realloc an array of structs -
using c, i'm trying input stuff array of structs, , once array filled, double size of array , keep going, using realloc.
i know there's been several question asked already, hoping explain since didn't create array way questions did , getting bit confused.
i have struct
struct data { // variables }
and initialised array using
struct data entries[100]; int curentries = 100; int counter = 1; // index, use (counter - 1) when accessing
to realloc, i'm using
if(counter == curentries){ // counter = index of array, curentries = total entries = realloc(entries, curentries * 2); }
i know need cast realloc right? i'm not sure how or i'm meant casting to, don't have anything, of course gives me error "assignment expression array type"
thanks!
struct data entries[100];// memory allocated
you need declare entries
pointer like:
struct data *entries=null; entries = malloc(curentries * sizeof(struct data)); //when time reallocate entries = realloc(entries, (curentries * 2 * sizeof(struct data)));
Comments
Post a Comment