CKTIS / C
EmployeeComparator: a comparator function pointer
Define a function-pointer alias and use it to choose at runtime how `qsort` orders an array of employee records.
Version: ISO C23; implementation support may vary
Syntax
typedef int (*EmployeeComparator)(const void *, const void *);
/* In a scope with a compatible function declaration: */
EmployeeComparator cmp = compare_by_id;
/* Equivalent declaration without the alias: */
int (*cmp_raw)(const void *, const void *) = compare_by_id;EmployeeComparator is a type name defined by the example, not a function or a name from the C standard library. typedef gives an existing type another name: here, a pointer to a function that takes two const void * arguments and returns int. The parentheses in (*EmployeeComparator) group * with the name, so EmployeeComparator cmp; declares a pointer variable without an extra *.
Assign a function with a compatible signature to cmp. In this expression, the function name converts to a pointer to that function, so & is optional. The ?: operator selects the pointer at runtime. Then qsort from <stdlib.h> takes the array, the element count, the size of one element in bytes, and cmp. It sorts the array in place and returns no value.
qsort passes the addresses of two elements. In the comparator, assign the arguments to const Employee * variables before reading the fields. The pointers must actually refer to Employee elements; do not modify the elements being compared. A negative result, zero, or a positive result means the first element is respectively less than, equal to, or greater than the second under the chosen ordering. Comparisons must give a consistent ordering regardless of where the elements currently sit in the array.
For int values, (a > b) - (a < b) gives -1, 0, or 1 without the overflow risk of a - b. qsort does not guarantee that elements comparing equal keep their relative order, so the example compares id when score ties. The syntax is valid in ISO C23 and does not rely on features introduced in C23; support for C23 mode depends on the implementation.
Examples
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int id;
int score;
} Employee;
typedef int (*EmployeeComparator)(const void *, const void *);
static int compare_int(int a, int b)
{
return (a > b) - (a < b);
}
static int compare_by_id(const void *left, const void *right)
{
const Employee *a = left;
const Employee *b = right;
return compare_int(a->id, b->id);
}
static int compare_by_score(const void *left, const void *right)
{
const Employee *a = left;
const Employee *b = right;
int order = compare_int(a->score, b->score);
return order != 0 ? order : compare_int(a->id, b->id);
}
int main(void)
{
Employee employees[] = {{3, 70}, {1, 90}, {2, 70}};
const size_t count = sizeof employees / sizeof employees[0];
for (int by_score = 0; by_score < 2; ++by_score) {
EmployeeComparator cmp = by_score ? compare_by_score : compare_by_id;
qsort(employees, count, sizeof employees[0], cmp);
printf("%s:", by_score ? "score" : "id");
for (size_t i = 0; i < count; ++i) {
printf(" %d", employees[i].id);
}
putchar('\n');
}
return 0;
}
Result
id: 1 2 3 score: 2 3 1
The program sorts the same array twice and prints the IDs: first by `id`, then by `score`, with ties broken by `id`.
Related lessons
- C · Lesson 54 · Intermediate · Version: 3