2010/12/30

C Function Pointer

#include <stdio.h>

// a function which has
//        input as float,
//        output as a function pointer, points to a function which has:
//            input as int,
//            output as void.
typedef void (*funcptr(float))(int);

// a function pointer, points to a function which has:
//        input as int,
//        output as void.
typedef void (*funcptr2)(int);

// a function pointer, points to a function which has:
//        input as float,
//        output as function pointer, points to a function which has:
//            input as int,
//            output as void.
typedef void (*(*funcptr3)(float))(int);

funcptr2 xxx(float);
void yyy(int);

funcptr2 xxx(float val)
{
    printf("xxx desu\n");
    return yyy;
}

void yyy(int val)
{
    printf("yyy desu\n");
}

int main(int argc, char **agrv)
{
    funcptr *ptr = xxx;
    funcptr2 ptr2 = ptr(0.0);
    ptr2(0);

    funcptr3 ptr3 = xxx;
    funcptr2 ptr4 = ptr3(0.0);
    ptr4(0);

    void (*(*ptr5)(float))(int) = xxx;
    void (*ptr6)(int) = ptr5(0.0);
    ptr6(0);

    // note: for function pointers, ptr(param) equals to (*ptr)(param)

    return 0;
}