I want to create an ArrayList, or Vector in C. Perhaps I can learn if I'm on the right path or completely off base
So if I have an ArrayList Struct which contains an array, initially set to 10, and a counter to keep track of how many elements have been filled in the arraylist, like such
typedef struct ArrayList
{
int counter;
int arr[10];
}
Could the array arr, be replaced by another array that is twice the size of the original array? If so, how would I do that?
I have the following piece of code in an add() Function
if ( arrList->counter == (sizeof(arrList->arr)/sizeof(int)) )
{
int tempArray[((arrList->counter + 1) * 2)];
for (int i = 0; i < arrList->counter; i++)
{
tempArray[i] = arrList->arr[i];
}
strcpy( arrList->arr, tempArray );
}
Am I on the right path or is there a better way to create a growable array?