for some unknown reason this has had me intrigued. i've revised my original solution and packaged it up. as always, i haven't tested it, but here it is...
Code:
int compare_int(int *x, int *y)
{
count();
if(x < y)
return -1;
if(x > y)
return +1;
return 0;
}
int * count(void)
{
static int counter = 0;
counter++;
return &counter;
}
int qsort2(int *a, int b, int c, int (*d)(int, int))
{
int temp;
int *p = count(); //get address of counter in count function
*p = 0; //initialize it to 0 because we just made it count up 1
qsort(a,b,c,d); //call quick sort which will call compare int
//which will call count
temp = *p; //move to temp so we can reset counter to 0
*p = 0; //..so that other functions can use the count()
//without having to initialize counter to 0
return temp;
}
int main()
{
int comparisons;
comparisons = qsort2(w,x,y,compare_int);
printf("There were %d comparisons made\n", comparisons);
}