summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2016-04-13 14:29:48 +1200
committerTom Ryder <tom@sanctum.geek.nz>2016-04-13 14:29:48 +1200
commit2b22b9bece9c286952123b648a51ebf0b29115be (patch)
tree6ec92303281f32b18fba1e476d3c3079bf0a09dc
parentTinkering with function pointers (diff)
downloadfuncptr-2b22b9bece9c286952123b648a51ebf0b29115be.tar.gz
funcptr-2b22b9bece9c286952123b648a51ebf0b29115be.zip
Making vectors of vectors
-rw-r--r--.gitignore1
-rw-r--r--Makefile4
-rw-r--r--funcptrptr.c78
3 files changed, 81 insertions, 2 deletions
diff --git a/.gitignore b/.gitignore
index d547342..be02820 100644
--- a/.gitignore
+++ b/.gitignore
@@ -1 +1,2 @@
funcptr
+funcptrptr
diff --git a/Makefile b/Makefile
index 74fe256..87db275 100644
--- a/Makefile
+++ b/Makefile
@@ -3,7 +3,7 @@
CC = clang
CFLAGS = -std=c90 -Weverything
-all : funcptr
+all : funcptr funcptrptr
clean :
- rm -f funcptr
+ rm -f funcptr funcptrptr
diff --git a/funcptrptr.c b/funcptrptr.c
new file mode 100644
index 0000000..fdc1542
--- /dev/null
+++ b/funcptrptr.c
@@ -0,0 +1,78 @@
+#include <stdio.h>
+#include <stdlib.h>
+
+int doub(int);
+int trip(int);
+int half(int);
+void fpv_exec(int (**fpv)(int), int n);
+void fpvv_exec(int (***fpvv)(int), int n);
+void fpvvv_exec(int (****fpvvv)(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));
+ }
+}
+
+void fpvv_exec(int (***fpvv)(int), int n) {
+ int i;
+ for (i = 0; *(fpvv + i) != NULL; i++) {
+ fpv_exec(*(fpvv + i), n);
+ }
+}
+
+void fpvvv_exec(int (****fpvvv)(int), int n) {
+ int i;
+ for (i = 0; *(fpvvv + i) != NULL; i++) {
+ fpvv_exec(*(fpvvv + i), n);
+ }
+}
+
+int main(int argc, char **argv) {
+ int (****fpvvv)(int);
+ fpvvv = calloc(2, sizeof(int (***)(int)));
+
+ fpvvv[0] = calloc(2, sizeof(int (**)(int)));
+ fpvvv[1] = calloc(2, sizeof(int (**)(int)));
+
+ fpvvv[0][0] = calloc(4, sizeof(int (*)(int)));
+ fpvvv[0][1] = calloc(4, sizeof(int (*)(int)));
+ fpvvv[1][0] = calloc(4, sizeof(int (*)(int)));
+ fpvvv[1][1] = calloc(4, sizeof(int (*)(int)));
+
+ fpvvv[0][0][0] = doub;
+ fpvvv[0][0][1] = trip;
+ fpvvv[0][0][2] = half;
+ fpvvv[0][0][3] = NULL;
+ fpvvv[0][1][0] = doub;
+ fpvvv[0][1][1] = trip;
+ fpvvv[0][1][2] = half;
+ fpvvv[0][1][3] = NULL;
+ fpvvv[1][0][0] = doub;
+ fpvvv[1][0][1] = trip;
+ fpvvv[1][0][2] = half;
+ fpvvv[1][0][3] = NULL;
+ fpvvv[1][1][0] = doub;
+ fpvvv[1][1][1] = trip;
+ fpvvv[1][1][2] = half;
+ fpvvv[1][1][3] = NULL;
+
+ for (argv++, argc--; argc; argv++, argc--) {
+ fpvvv_exec(fpvvv, atoi(*argv));
+ }
+
+ exit(EXIT_SUCCESS);
+}