Need Some Help with function pointers

jsnaik's Avatar, Join Date: Dec 2007
Newbie Member
How do u declare a function pointer which takes a void pointer and returns a void pointer.

so the function pointer should point to something like

void * process(void * data)
{

}

i tried sthing like

(void *) (function *)(void *)

but doesnt work!!!

opinions and view please ( even solutions will do... )
0
Salem's Avatar, Join Date: Nov 2007
Ambitious contributor
You were pretty close!
Code:
#include <stdio.h>

void *foo ( void *msg ) {
    printf( "%s\n", msg );
    return msg;
}

typedef void *(*fnptr)(void*);

int main ( ) {
    void * (*f1)(void*) = foo;
    fnptr   f2 = foo;
    f1( "hello" );
    f2( "world" );
    return 0;
}
I would recommend the typedef approach if you're going to be using the same function pointer type a lot. Your code will become very verbose otherwise.

Also read this - http://www.newty.de/fpt/index.html