Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C

EmpCmp: an array of comparator function pointers in C

Define a function pointer type for employee comparisons and indirectly call the four active comparators in an array with capacity for eight.

Version: ISO C23; implementation support may vary

Syntax

typedef struct Employee Employee;
typedef int (*EmpCmp)(const Employee *, const Employee *);

/* With the four compatible functions already declared: */
EmpCmp comparators[8] = {by_id, by_department, by_salary, by_years};

EmpCmp is a typedef name for a pointer to a function that takes two const Employee * arguments and returns int. It is an alias for the pointer type, not a function or a distinct new type. The parentheses around *EmpCmp make the declaration read as a pointer to a function.

EmpCmp comparators[8] is an array of eight function pointers. In this initializer, each compatible function name converts to a pointer to that function. Indices 0–3 hold the four active pointers. The omitted elements at indices 4–7 are initialized to null pointers. Capacity is not the number of active entries.

comparators[i](&a, &b) indirectly calls the function selected by i. The equivalent explicit form is (*comparators[i])(&a, &b). Keep the index in bounds and call only non-null pointers to compatible functions. Calling through a null pointer or through a pointer to an incompatible function type has undefined behavior. A cast does not make an incompatible call safe.

By convention here, a negative result means a comes before b, zero means they tie on the selected field, and a positive result means a comes after b. The EmpCmp type specifies the signature but does not enforce that convention. This pattern does not require features introduced in C23; C23 support varies by implementation.

Examples

#include <stdio.h>

typedef struct Employee {
    int id;
    int department;
    int salary;
    int years;
} Employee;

typedef int (*EmpCmp)(const Employee *, const Employee *);

static int cmp_int(int a, int b)
{
    return (a > b) - (a < b);
}

static int by_id(const Employee *a, const Employee *b)
{
    return cmp_int(a->id, b->id);
}

static int by_department(const Employee *a, const Employee *b)
{
    return cmp_int(a->department, b->department);
}

static int by_salary(const Employee *a, const Employee *b)
{
    return cmp_int(a->salary, b->salary);
}

static int by_years(const Employee *a, const Employee *b)
{
    return cmp_int(a->years, b->years);
}

int main(void)
{
    const Employee a = {101, 2, 7000, 5};
    const Employee b = {102, 2, 6500, 8};
    enum { active_count = 4 };
    EmpCmp comparators[8] = {by_id, by_department, by_salary, by_years};
    const char *const labels[active_count] = {"id", "department", "salary", "years"};

    for (int i = 0; i < active_count; ++i) {
        printf("%s: %d\n", labels[i], comparators[i](&a, &b));
    }

    return 0;
}

Result

id: -1
department: 0
salary: 1
years: -1

The loop compares the same two records four ways without sorting them. `department: 0` means only that the employees have the same department number.

Related lessons

  • C · Lesson 55 · Intermediate · Version: 3

Sources