CKTIS / C
Predicate: arrays of predicate callbacks in C
Define `Predicate` as a function pointer type alias, then use an array of callbacks to check whether one order satisfies every rule or at least one rule.
Version: ISO C23; implementation support may vary
Syntax
typedef struct Order Order;
typedef int (*Predicate)(const Order *);
/* After declaring two functions with compatible types: */
const Predicate checks[] = {is_large, is_paid};Predicate is a programmer-defined alias for a function pointer type, not a standard C name. typedef int (*Predicate)(const Order *); names a pointer to a function that takes one const Order * and returns int. The parentheses around *Predicate make the alias a pointer type. By convention, 0 means false and any nonzero value means true. The callback cannot modify the order through that pointer, but the type does not rule out other side effects.
const Predicate checks[] = {is_large, is_paid}; creates an array whose function pointers cannot be reassigned. The function names in the initializer convert to pointers. checks[i](order) calls the callback at index i on the same order. As a function parameter, const Predicate checks[] is adjusted to a pointer to an element, so pass the count n separately. Compute sizeof checks / sizeof checks[0] where checks is an array, not inside the helper.
all_match stops at the first zero result; any_match stops at the first nonzero result. Each helper checks rules in array order, skips the rest once the answer is known, and returns 0 or 1. With n == 0, neither calls a callback: all_match returns 1 and any_match returns 0. all_of and any_of are not ISO C library functions; the example defines its own helpers.
For n > 0, pass an array with at least n elements, non-null pointers to functions of compatible type, and a pointer to an order that stays alive throughout the call. Calling through a null function pointer or a pointer to a function of incompatible type is undefined behavior. This technique predates C23; support for C23 mode itself varies by implementation.
Examples
#include <stddef.h>
#include <stdio.h>
typedef struct Order {
int total;
int paid;
} Order;
typedef int (*Predicate)(const Order *);
static int is_large(const Order *order)
{
return order->total >= 100;
}
static int is_paid(const Order *order)
{
return order->paid;
}
static int all_match(const Predicate checks[], size_t n, const Order *order)
{
for (size_t i = 0; i < n; ++i) {
if (!checks[i](order)) {
return 0;
}
}
return 1;
}
static int any_match(const Predicate checks[], size_t n, const Order *order)
{
for (size_t i = 0; i < n; ++i) {
if (checks[i](order)) {
return 1;
}
}
return 0;
}
int main(void)
{
const Order order = {40, 2};
const Predicate checks[] = {is_large, is_paid};
const size_t n = sizeof checks / sizeof checks[0];
const int all = all_match(checks, n, &order);
const int any = any_match(checks, n, &order);
printf("mixed: all=%d any=%d\n", all, any);
printf("empty: all=%d any=%d\n",
all_match(NULL, 0, &order), any_match(NULL, 0, &order));
return 0;
}
Result
mixed: all=0 any=1 empty: all=1 any=0
For `{40, 2}`, `all_match` stops when `is_large` fails. `any_match` continues to `is_paid`, whose result `2` counts as true. The calls with `NULL` and a count of `0` do not read the array.
Related lessons
- C · Lesson 56 · Intermediate · Version: 3