From 764b6e2a3e8052ff2b4ce732963e6add97aa5291 Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Mon, 28 Mar 2016 16:45:13 +1300 Subject: Tinkering with function pointers --- funcptr.c | 43 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) create mode 100644 funcptr.c (limited to 'funcptr.c') 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 +#include + +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); +} + -- cgit v1.2.3