diff options
Diffstat (limited to 'funcptr.c')
-rw-r--r-- | funcptr.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/funcptr.c b/funcptr.c new file mode 100644 index 0000000..de77066 --- /dev/null +++ b/funcptr.c @@ -0,0 +1,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); +} + |