Is it possible to replicate an generic array in pure ANSI-C?
I have this struct which holds an array (for floats at the moment) and some variables like size and capacity for mutation in the array.
typedef struct _CustomArray
{
float* array; //the array in which the objects will be stored
int size; //the current size of the array
int capacity; //the max capacity of the array
} CustomArray;
I use this struct so I can make an array in pure C where I can add/remove items, dynamically expand the array size when needed etc. all the things a "standard" array does, except it is made in C only. And now I want to make this so that when you initialize this struct you can set the datatype of the elements it should hold, at this moment it's only capable of storing float datatypes, but I want to make it so that it can store any datatype/other structs. But I don't know if this is even possible.
At this moment the function to make this array is:
CustomArray* CustomArray_Create(int initCapacity, /*type elementType*/)
{
CustomArray* customArray_ptr; //create pointer to point at the structure
float* internalArray = (float*)malloc(sizeof(float) * initCapacity); //create the internal array that holds the items
if(internalArray != NULL)
{
CustomArray customArray = { internalArray, 0, initCapacity }; //make the struct with the data
customArray_ptr = &customArray; //get the adress of the structure and assign it to the pointer
return customArray_ptr; //return the pointer
}
return NULL;
}
Is it possible to give a datatype as parameter so I can malloc memory for that datatype and cast it as that given datatype in an array dynamically?
Thanks in advance,
Marnix van Rijswijk