CKTIS / C
Predicate function pointer `int (*pred)(const char *)`
Pass a string-selection rule as a function pointer so the same filter can count matching strings first and then collect them in input order.
Version: ISO C23; implementation support may vary
Syntax
/* In a function body, with starts_with_a declared and s pointing to a valid C string: */
int (*pred)(const char *) = starts_with_a;
int result = pred(s);Start at pred: the parentheses in (*pred) group the name with *. The declaration int (*pred)(const char *) makes pred a pointer to a function that takes one const char * and returns int. Without those parentheses, int *pred(const char *) declares a function returning int *. pred points to a function, not a data object; const qualifies the characters, not the pointer.
Pass a compatible function as starts_with_a or &starts_with_a; both give the same pointer. pred(s) and (*pred)(s) call the same function. Calling through a null pointer or through an incompatible function-pointer type is undefined behavior; a cast does not make the types compatible. The character pointer alone does not guarantee a valid string: this filter requires live, \0-terminated strings, and the characters cannot be modified through s. This syntax works in ISO C23 but predates it; C23 mode support varies by implementation.
The filter treats 0 as reject and any nonzero value as keep. The first pass counts matches; the second stores their pointers in input order. The input and selection rule must yield the same decision for each element in both passes. The size checks protect the buffer but cannot detect a different set of matches with the same count. The result borrows pointers rather than copying strings: the caller frees the result array and keeps the strings alive while using it.
Examples
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
static int starts_with_a(const char *s)
{
return s[0] == 'a';
}
/* Requires live C strings and a predicate with stable decisions. */
static int filter(const char *const items[], size_t n,
int (*pred)(const char *),
const char ***out, size_t *out_count)
{
*out = NULL;
*out_count = 0;
if (pred == NULL)
return 0;
size_t count = 0;
for (size_t i = 0; i < n; ++i)
if (pred(items[i]))
++count;
if (count == 0)
return 1;
const char **matches;
if (count > SIZE_MAX / sizeof *matches)
return 0;
matches = malloc(count * sizeof *matches);
if (matches == NULL)
return 0;
size_t used = 0;
for (size_t i = 0; i < n; ++i) {
if (pred(items[i])) {
if (used == count) {
free(matches);
return 0;
}
matches[used++] = items[i];
}
}
if (used != count) {
free(matches);
return 0;
}
*out = matches;
*out_count = used;
return 1;
}
int main(void)
{
const char *const items[] = {"apple", "pear", "", "apricot"};
const char **matches;
size_t count;
if (!filter(items, sizeof items / sizeof items[0],
starts_with_a, &matches, &count)) {
fputs("filter failed\n", stderr);
return EXIT_FAILURE;
}
printf("matches: %zu\n", count);
for (size_t i = 0; i < count; ++i)
puts(matches[i]);
free(matches);
return EXIT_SUCCESS;
}
Result
matches: 2 apple apricot
`starts_with_a` selects `apple` and `apricot`; the empty string starts with `\0`. The `filter` status `1` means success, including no matches; `0` means failure. That status is separate from the predicate result.
Related lessons
- C · Lesson 24 · Intermediate · Version: 3