The main problem is that you are trying to use realloc with a stack allocated array. You have:
ent a[mSize];
This is an automatic distribution on the stack. If you want to use realloc () for this later, you must create an array on the heap using malloc (), for example:
ent *a = (ent*)malloc(mSize * sizeof(ent));
So, the malloc library (and therefore realloc (), etc.) knows about your array. In appearance, you can mislead variable-length arrays with variable-length arrays with true dynamic arrays , so make sure you understand the difference there before trying to fix it.
Indeed, if you write dynamic arrays in C, you should try using the OOP-ish construct to encapsulate information about your arrays and hide it from the user. You want to consolidate information (for example, pointer and size) about your array into structure and operations (for example, selection, adding elements, deleting elements, deallocation, etc.) into special functions that work with your structure. So you can:
typedef struct dynarray { elt *data; int size; } dynarray;
And you can define some functions for working with dynarrays:
Thus, the user does not need to remember exactly how to distribute things or what size of the array is currently. Hope you get started.
source share