diff options
Diffstat (limited to 'funcptrs.c')
| -rw-r--r-- | funcptrs.c | 41 | 
1 files changed, 41 insertions, 0 deletions
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; +} +  | 
