summaryrefslogtreecommitdiff
path: root/funcptr.c
blob: de770669fb359b01c54aa0ce41a1317bc9847ebe (plain) (blame)
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
#include <stdio.h>
#include <stdlib.h>

int doub(int);
int trip(int);
int half(int);
void fpv_exec(int (**fpv)(int), int n);

int doub(int i) {
    return i * 2;
}

int trip(int i) {
    return i * 3;
}

int half(int i) {
    return i / 2;
}

void fpv_exec(int (**fpv)(int), int n) {
    int i;
    for (i = 0; *(fpv + i) != NULL; i++) {
        printf("%d\n", (*(fpv + i))(n));
    }
}

int main(int argc, char **argv) {
    int (**fpv)(int);
    fpv = calloc(4, sizeof(int (*)(int)));

    fpv[0] = doub;
    fpv[1] = trip;
    fpv[2] = half;
    fpv[3] = NULL;

    for (argv++, argc--; argc; argv++, argc--) {
        fpv_exec(fpv, atoi(*argv));
    }

    exit(EXIT_SUCCESS);
}