I’m creating my personal generic library with function and data structures in C.
I have a generic vector
typedef struct vector
{
int max;
int size;
void **data;
} vector_t;
and I created functions for vector:
int vector_new( vector_t * v, int vecsize );
int vector_add( vector_t *v , void *elem );
int vector_remove( vector_t * v, void *elem);
void vector_free( vector_t *v );
In the implementation of the remove function, I add the element if and only if the element is not already present in the array of data.
I created a function search (because I don’t want to sort the element that I insert) to looking for possible duplicates.
Well, I have doubts about the type of 1st parameter that I have to pass to the function. I want to create the function search not only for this use, but general.
In remove() I called it like this: search(v->data, elem);,
but how will be the prototype of the function?
int search( ??? , void * e );
I know the 1st parameter could be an array. But I don’t know if a void* or a void** for example.
The important is that this function works not just for the struct and functions I have created.
It sounds like you need to define a function pointer for comparing elements in the vector for equality. This can then be added as a parameter to the search method
Now lets say I had a vector which contained
intvalues. I could define my callback as so