fp.c


/*********************************************************************
Build with:

cl [-Zi] fp.c

using Zi will produce a debuggable executable.
*********************************************************************/
#include <stdio.h>

void func1(void){
    printf("In func1\n");
    return;
}

void func2(void){
    printf("In func2\n");
    return;
}

/*
    The typedef keyword introduces a new data type.  In this case,
    the data type has the name p_vv_fn.  Variables of type p_vv_fn are
    pointers to functions.  Assigning the address of a function to a
    function pointer will allow you to call through the function
    pointer into the function it (the pointer) addresses.
*/
typedef void (*p_vv_fn)(void);

int main(int argc, char *argv[]){
    /*
        Declare a variable using the data type we just introduced.
        The variable is a pointer to a function named "func1".
        Calling through the function pointer will result in executing
        the function named "func1".
    */
    p_vv_fn func_ptr = func1;
    func_ptr();

    /*
        Assign the address of the function "func2" to our pointer.
        Calling through the pointer now causes us to execute the
        function "func2".
    */
    func_ptr = func2;
    func_ptr();
    
    return 0;
}