I would like to get a reference to an element at position in an array of void pointers. This is my function, I don't know if am doing it correctly. I have these two functions:
typedef struct vector
{
void **array;
size_t size;
}vector;
I believe this gets the reference to the element at position pos.
void **get_at(vector *this, size_t pos)
{
assert(pos >=0 && pos < this->size);
return &this->array[pos];
}
I believe this gets the pointer to the element at position pos.
void *at(vector *this, size_t pos)
{
assert(pos >=0 && pos < this->size);
return this->array[pos];
}
I just would like to know if am correct with this implementation.
Just one more question: If I want to destroy the array, is doing it this way enough? Do I have to loop through the array elements freeing the memory held by each element?
void destroy(vector *this)
{
free(this->array);
free(this);
}