diff options
-rw-r--r-- | Makefile | 2 | ||||
-rw-r--r-- | funcptrs.c | 41 | ||||
-rwxr-xr-x | myls.sh | 9 |
3 files changed, 51 insertions, 1 deletions
@@ -1,7 +1,7 @@ BINS = ascii class depipe_strings dup fpipe pipe realloc strpbrk strsep \ tokenizer getopt prime_mask linked_list pi_bbp string_tokenizer \ tcpserver tcpclient regexp c99 asm playground_05-25-2011 \ - parent_to_child + parent_to_child funcptrs CURL = curl_tomem curl_tofile diff --git a/funcptrs.c b/funcptrs.c new file mode 100644 index 0000000..1d49418 --- /dev/null +++ b/funcptrs.c @@ -0,0 +1,41 @@ +#include <stdio.h> +#include <stdlib.h> + +void say1(void) +{ + printf("hello, world!\n"); +} + +void say2(void) +{ + printf("hello bro!\n"); +} + +/* quite silly function, it is better to return an int and modify a parameter */ +void (*swap_say( void (*current_say)(void) )) (void) +{ + if (!current_say) + return NULL; + + void (*ret)(void) = NULL; + + if (current_say == &say1) + ret = say2; + else + ret = say1; + + return ret; +} + +int main(int argc, char **argv) +{ + void (*say_fptr)(void) = say1; + say_fptr(); + + /* now modfiy the above func ptr */ + say_fptr = swap_say(say_fptr); + say_fptr(); + + return 0; +} + @@ -0,0 +1,9 @@ +#!/bin/bash + +LIST=(*) +ARR_SZ=${#LIST[@]} +for ((i = 0; i < ARR_SZ; i++)) +do + echo "${LIST[$i]}" +done; + |