1
votes

I have made the following typedefs in my program (C):

typedef void* ListElement;

typedef int(*CompareListElements)(ListElement, ListElement);

i have made a function pointer in my code: CompareListElements compareElement

Later in the code i wish to use qsort on an array of ListElements:

qsort(elementsArray,listGetSize(list),sizeof(list->dummyHead->next->element, compareElement);

However the compiler states: "passing argument 4 of 'qsort' from incompatible pointer type".

I fear that it is because the qsort requires a function in the format of int (const void*, const void*). when i supply int (void*, void*).

Is there a way of casting the arguments of compareElement to (const void*, const void*), while calling qsort or before, WITHOUT changing the typedef?

Thanks

1

1 Answers

0
votes

Simply cast the pointer to the appropriate type.

typedef int(*ConstCompareListElements)(const void *, const void *);

qsort(elementsArray,listGetSize(list),sizeof(list->dummyHead->next->element, 
         (ConstCompareListElements)compareElement);