I want to pass an array of arbitrary struct pointers and a comparison function to a generic sorting algorithm. Is this possible in C?
The goooeys of the structs would only be accessed within the comparison function, the sorting function would only need to call the comparison function and swap pointers, but I can’t figure out how to declare it.
function sorter( struct arbitrary ** Array, int Length, int cmp(struct node * a, struct node * b))
{
for (int i=0; i<Length;i++){
if cmp(Array[i],Array[i+1]){
swap(Array[i],Array[i+1]
}
}
}
You can declare the function as:
Inside of the comparison function, you will then need to cast the two pointers being compared to pointers to whatever struct type the comparison function compares.