summaryrefslogtreecommitdiffstats
path: root/funcptrs.c
blob: 1d49418658432d4c97f095bab7dfb7cfc5809834 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
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;
}