CKTIS / C
Function pointer declarators and functions returning function pointers
Read outward from the name to tell a variable that holds a comparator pointer from a function that returns one.
Version: ISO C23; implementation support may vary
Syntax
int (*cmp)(const void *, const void *);
int (*get_comparator(const char *))(const void *, const void *);
/* Equivalent declaration of get_comparator using a pointer type alias: */
typedef int (*comparator_fn)(const void *, const void *);
comparator_fn get_comparator(const char *);The distinction between a function pointer declarator and function returning function pointer is easiest to see by starting at the name. In int (*cmp)(const void *, const void *);, (*cmp) makes cmp a pointer. The list on the right describes the pointed-to function: it takes two const void * arguments and returns int. Without the grouping parentheses, int *cmp(const void *, const void *); instead declares a function returning int *, not a variable holding a function pointer.
In int (*get_comparator(const char *))(const void *, const void *);, read get_comparator(const char *) first: it is a function taking a const char *. It returns a pointer to a function taking two const void * arguments and returning int. C cannot return a function directly, but it can return a pointer to one. The typedef shown above names the pointer type comparator_fn, so comparator_fn get_comparator(const char *); declares the same function. This syntax predates ISO C23; support for C23 mode varies by implementation.
In return descending;, the function name converts to a pointer to that function; return &descending; returns the same pointer. If the selector can return NULL, check its result before passing it to qsort or calling the comparator as cmp(a, b) or (*cmp)(a, b). The pointed-to function must have a type compatible with the type used for the call. Casting a pointer does not make an incompatible call valid; such a call has undefined behavior.
qsort from <stdlib.h> passes addresses of array elements to the comparator. A negative, zero, or positive result means the first element sorts before, is equivalent in order to, or sorts after the second. In this example the const void * arguments point to live int objects; converting them to const int * lets the comparator read the values without modifying them. The expression (a > b) - (a < b) supplies the right sign without the risk of signed overflow from a - b.
Examples
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
static int ascending(const void *lhs, const void *rhs)
{
int a = *(const int *)lhs;
int b = *(const int *)rhs;
return (a > b) - (a < b);
}
static int descending(const void *lhs, const void *rhs)
{
return ascending(rhs, lhs);
}
static int (*get_comparator(const char *order))(const void *, const void *)
{
if (strcmp(order, "asc") == 0)
return ascending;
if (strcmp(order, "desc") == 0)
return descending;
return NULL;
}
int main(void)
{
int values[] = {4, 1, 3};
int (*cmp)(const void *, const void *) = get_comparator("desc");
if (cmp == NULL)
return EXIT_FAILURE;
qsort(values, sizeof values / sizeof values[0], sizeof values[0], cmp);
printf("%d %d %d\n", values[0], values[1], values[2]);
return 0;
}Result
4 3 1
`get_comparator("desc")` selects `descending`, which reverses the order by swapping the arguments to `ascending`. An unknown name returns `NULL`; the check before `qsort` handles that case. The expected output is `4 3 1`.
Related lessons
- C · Lesson 14 · Intermediate · Version: 3