From 71d6c5d17da5162255e77ce2d6e0c44d4db4698e Mon Sep 17 00:00:00 2001 From: Tom Ryder Date: Fri, 26 Jan 2018 14:27:45 +1300 Subject: Re-use passed pointers for cheap iteration --- funcptr.c | 14 ++++---------- 1 file changed, 4 insertions(+), 10 deletions(-) diff --git a/funcptr.c b/funcptr.c index 478beca..33d4154 100644 --- a/funcptr.c +++ b/funcptr.c @@ -22,18 +22,12 @@ int half(int i) { static int (*fpv[])(int) = {doub, trip, half, NULL}; void fpv_exec(int (**fpv)(int), int n) { - int i; - for (i = 0; *(fpv + i) != NULL; i++) { - printf("%d\n", (*(fpv + i))(n)); - } + while (*fpv) + printf("%d\n", (*fpv++)(n)); } int main(int argc, char **argv) { - int i; - for (i = 1; i < argc; i++) { - fpv_exec(fpv, atoi(argv[i])); - } - + for (argv++, argc--; argc; argv++, argc--) + fpv_exec(fpv, atoi(*argv)); exit(EXIT_SUCCESS); } - -- cgit v1.2.3