summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTom Ryder <tom@sanctum.geek.nz>2019-12-10 16:13:48 +1300
committerTom Ryder <tom@sanctum.geek.nz>2019-12-10 16:13:48 +1300
commit80993355ab2244bae06ea170d8cf6f98eb3300e5 (patch)
tree357787110697e4b6622dab0a209b1e4aed4c90bf
parentUse more idiomatic vector iteration (diff)
downloadfuncptr-80993355ab2244bae06ea170d8cf6f98eb3300e5.tar.gz
funcptr-80993355ab2244bae06ea170d8cf6f98eb3300e5.zip
Adjust implementation
-rw-r--r--funcptrptr.c39
1 files changed, 13 insertions, 26 deletions
diff --git a/funcptrptr.c b/funcptrptr.c
index f52719d..3d2fe69 100644
--- a/funcptrptr.c
+++ b/funcptrptr.c
@@ -1,5 +1,6 @@
#include <stdio.h>
#include <stdlib.h>
+#include <string.h>
int doub(int);
int trip(int);
@@ -40,32 +41,18 @@ void fpvvv_exec(int (****fpvvv)(int), int 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;
+ int (*fpv[])(int) = {doub, trip, half, NULL};
+ int i, j, im, jm;
+ im = jm = 2;
+
+ fpvvv = calloc(im, sizeof *fpvvv);
+ for (i = 0; i < im; i++) {
+ fpvvv[i] = calloc(jm, sizeof **fpvvv);
+ for (j = 0; j < jm; j++) {
+ fpvvv[i][j] = malloc(sizeof fpv);
+ memcpy(fpvvv[i][j], fpv, sizeof fpv);
+ }
+ }
for (argv++, argc--; argc; argv++, argc--)
fpvvv_exec(fpvvv, atoi(*argv));