summaryrefslogtreecommitdiff
path: root/funcptr.c
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-03-28 16:45:13 +1300
committerTom Ryder <tom@sanctum.geek.nz>2016-03-28 16:45:13 +1300
commit764b6e2a3e8052ff2b4ce732963e6add97aa5291 (patch)
treea03f6793c8ec9cda541620e5bcaa23779e9c5ed5 /funcptr.c
downloadfuncptr-764b6e2a3e8052ff2b4ce732963e6add97aa5291.tar.gz
funcptr-764b6e2a3e8052ff2b4ce732963e6add97aa5291.zip
Tinkering with function pointers
Diffstat (limited to 'funcptr.c')
-rw-r--r--funcptr.c43
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);
+}
+