The comparison function is used to compare elements of a queue. Since the queue routines keep track of pointers to objects and know nothing about the data type of those objects, the programmer must write the comparison function that will work with the type of objects being stored in the queue.
The comparison function should accept two pointers as arguments and return a value less than zero if the first object is less than the second, zero if the two objects are equal, and a value greater than zero if the first is greater than the second.
The following is an example of a comparison function. Note that this queue is being used to store objects of type MYSTRUCT:
long comp(ptr1, ptr2) void *ptr1; void *ptr2; { struct MYSTRUCT *p1; struct MYSTRUCT *p2; p1 = (struct MYSTRUCT *)ptr1; p2 = (struct MYSTRUCT *)ptr2; if (p1->value < p2->value) return(-1); else if (p1->value == p2->value) return(0); else return(1); }