1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include <stdio.h>
#include <stdlib.h>
#include <stdbool.h>
#include <assert.h>
#include <string.h>  // for strcmp(…)


void print_compare_result(void* a, void* b, int(*compare_fn)(void*,void*)) {
    if(compare_fn(a, b) < 0) {
        printf("a comes before b\n");
    }
    else if(compare_fn(a, b) == 0) {
        printf("a is equivalent to b\n");
    }
    else {
        printf("a comes after b\n");
    }
}


int compare_int(void* a_n1, void* a_n2) {
    int* a_n1_as_address_of_int = a_n1;
    int* a_n2_as_address_of_int = a_n2;
    int n1 = *a_n1_as_address_of_int;
    int n2 = *a_n2_as_address_of_int;
    if(n1 < n2) {
        return -1;
    }
    else if(n1 == n2) {
        return 0;
    }
    else {
        return 1;
    }
}


int compare_str(void* a_lhs, void* a_rhs) {
    char* s1 = a_lhs;
    char* s2 = a_rhs;
    int strcmp_result = strcmp(s1, s2);
    if(strcmp_result < 0) {
        return -1;
    }
    else if(strcmp_result == 0) {
        return 0;
    }
    else {
        return 1;
    }
}


int main(int argc, char* argv[]) {
    int five  = 5;
    int seven = 7;
    print_compare_result(&five, &seven, compare_int);

    char* apple_str = "apple";
    char* banana_str = "banana";
    print_compare_result(banana_str, apple_str, compare_str);
    
    return EXIT_SUCCESS;
}
/* vim: set tabstop=4 shiftwidth=4 fileencoding=utf-8 noexpandtab: */

© Copyright 2023 Alexander J. Quinn         This content is protected and may not be shared, uploaded, or distributed.