CKTIS / C
Function pointer declarators and comparator callbacks in C
Declare a function pointer type, pass a compatible comparator as a callback, and compare signed integer priorities safely even at the limits of `int`.
Version: ISO C23; implementation support may vary
Syntax
/* Process is a previously declared type. */
typedef int (*ProcessCmp)(const Process *, const Process *);
/* Callback parameter using the typedef or the equivalent explicit declarator. */
int apply_cmp(const Process *a, const Process *b, ProcessCmp cmp);
int apply_cmp(const Process *a, const Process *b,
int (*cmp)(const Process *, const Process *));
/* Equivalent calls inside apply_cmp. */
cmp(a, b)
(*cmp)(a, b)The function pointer/callback declarator in typedef int (*ProcessCmp)(const Process *, const Process *); gives the pointer type, not the function type, the name ProcessCmp. The pointed-to function takes two pointers to const Process and returns int. The parentheses around *ProcessCmp matter: int (*f)(...) denotes a pointer to a function returning int, whereas int *f(...) denotes a function returning int *. Here ... stands for an omitted parameter list.
apply_cmp takes a ProcessCmp cmp parameter. Passing by_priority implicitly converts the function designator to a pointer; &by_priority works too. Inside apply_cmp, cmp(a, b) and (*cmp)(a, b) are equivalent. Pass a non-null pointer to a function of compatible type and pass pointers to Process objects that remain alive throughout the call. A cast does not make a call through an incompatible function type valid; that call has undefined behavior.
The comparator contract uses the sign: negative means the first key is less, zero means the keys compare equal, and positive means the first key is greater. INT_MIN and INT_MAX, from <limits.h>, are the smallest and largest int values. Using x - y to compare them can overflow signed int, which is undefined behavior. Instead, (x > y) - (x < y) subtracts only comparison results of type int that are 0 or 1, so it produces -1, 0, or 1 even at the extremes.
Unary negation in -r reverses the sign. It is safe for a result normalized to -1, 0, or 1, but an arbitrary comparator can return INT_MIN, whose negation has undefined behavior in C23. To reverse the order without negating the result, swap the arguments: cmp(b, a). The syntax is valid in ISO C23; implementation support for C23 may vary.
Examples
#include <limits.h>
#include <stdio.h>
typedef struct {
int priority;
} Process;
typedef int (*ProcessCmp)(const Process *, const Process *);
static int by_priority(const Process *a, const Process *b)
{
return (a->priority > b->priority) - (a->priority < b->priority);
}
static int apply_cmp(const Process *a, const Process *b, ProcessCmp cmp)
{
return cmp(a, b);
}
int main(void)
{
const Process low = { INT_MIN };
const Process high = { INT_MAX };
const Process same = { INT_MIN };
int ascending = apply_cmp(&low, &high, by_priority);
int equal = apply_cmp(&low, &same, by_priority);
int greater = apply_cmp(&high, &low, by_priority);
int reversed = -ascending;
printf("%d %d %d\n%d\n", ascending, equal, greater, reversed);
return 0;
}
Result
-1 0 1 1
The first line shows less, equal, and greater priority in that order. The second line shows the less-than result with its sign reversed. The equal priorities belong to distinct objects.
Related lessons
- C · Lesson 76 · Advanced · Version: 3