I am implementing a generic sort in C according to an assignment. In this assignment I need to get and sort (in bubble sort) an array of type void. The function signature is:
void bubbleSort(void* arrayToSort,int lenArray,size_t sizeElements, int (*compare)(const void*,const void*))
I got a peroblem: I find this error "expression must be a pointer to a complete object type" almost in everything I am doing. I searched, but all the advises about this error are to cast it first to the real type. I can't cast it, because the function is ment to be generic, and all I know is the size of the array's type. Can someone help me?
This is the code:
void bubbleSort(void* arrayToSort,int lenArray,size_t sizeElements, int (*compare)(const void*, const void*)) {
bool didExcanged = false;
for (int i = 0; i < lenArray && (!didExcanged); i++)
{
void* pivot = arrayToSort + i * sizeElements;//that error on "arrayToSort"
didExcanged = false;
for (int j = i; j < lenArray; j++)
{
void* toCompare = arrayToSort + j * sizeElements;//that error on "arrayToSort"
if (compare(pivot,toCompare)>0) {
swap(pivot, toCompare);
didExcanged = true;
}
}
}
}
and the swap function:
void swap(void* a, void* b) {
void *temp=*a;//that error on "*a"
*a = *b;//that error on "*a" and "*b"
*b = *temp;//that error on "*b" and "*temp"
}