1. This site uses cookies. By continuing to use this site, you are agreeing to our use of cookies. Learn More.

Need Some Help with function pointers

Discussion in 'C' started by jsnaik, Dec 6, 2007.

  1. jsnaik

    jsnaik New Member

    Joined:
    Dec 6, 2007
    Messages:
    2
    Likes Received:
    0
    Trophy Points:
    0
    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... ) ;)
     
  2. Salem

    Salem New Member

    Joined:
    Nov 15, 2007
    Messages:
    133
    Likes Received:
    5
    Trophy Points:
    0
    Occupation:
    Please don't PM me for 1:1 support.
    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
     

Share This Page