Go4Expert

Go4Expert (http://www.go4expert.com/)
-   C (http://www.go4expert.com/forums/c/)
-   -   Need Some Help with function pointers (http://www.go4expert.com/forums/help-function-pointers-t7738/)

jsnaik 6Dec2007 18:45

Need Some Help with function pointers
 
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... ) ;)

Salem 6Dec2007 21:26

Re: Need Some Help with function pointers
 
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


All times are GMT +5.5. The time now is 07:46.