Pointer to a function pointer

This is a discussion on Pointer to a function pointer within the C Programming forums, part of the General Programming Boards category; I'm trying to pass function pointers as arguments in a list of void pointers. However, ISO C forbids storing a ...

  1. #1
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591

    Pointer to a function pointer

    I'm trying to pass function pointers as arguments in a list of void pointers. However, ISO C forbids storing a function pointer as a void pointer, so I instead use the address of a function pointer variable, as so:
    Code:
    generic_func( void **args )
    {
     // generic stuff
    }
    
    void callback( void ) 
    {
      // specific stuff
    }
    
    int main( )
    {
      void (*func)(void) = callback;
      void *args[] = { &func, ... };
      generic_func( args );
    }
    The problem is with the syntax for casting a void pointer back into a pointer to a function pointer. I've tried the following:
    Code:
    void *arg = &func;
    *(unsigned __stdcall (*)(void *) *)arg...
    *(unsigned __stdcall (**)(void *))arg...
    The first doesn't parse correctly, the second does not dereference into the proper function pointer type. Can anyone suggest the correct syntax for this?
    Last edited by @nthony; 05-30-2010 at 01:46 AM.

  2. #2
    Registered User
    Join Date
    May 2010
    Location
    Naypyidaw
    Posts
    1,314
    You can wrap function pointer in a struct and pass struct address.
    Code:
    typedef struct func_wrapper {
        void (*call_back)(void);
    }func_wrapper;
    
    generic_func(func_wrapper args[ ] ) 
    {
        args[1].call_back();          /* call it */
    }
    
    int main(void)
    {
       func_wrapper args[] = { &callback , ... };
      generic_func(args);
      return 0;
    }

  3. #3
    Registered Abuser
    Join Date
    Jun 2006
    Location
    Toronto
    Posts
    591
    I used to use structs to pass args, but then you can't use generics.

    I did figure out the problem, and my c-syntax was mostly correctly; however, anyone using the win32 API will be keen to know that the problem was the association of __stdcall:
    Code:
    *(unsigned __stdcall (**)(void *))arg... // is wrong
    *(unsigned (__stdcall **)(void *))arg... // is correct
    Needless to say, that was frustrating.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Of course it is wrong. There's never a place in C where this is valid: (*)

    The star * indicates that you're either dereferencing a pointer, or declaring a pointer (since we're not talking about multiplication here). Pointers have to be tied to a specific type, so that you actually know what type of pointer it is actually pointing at. You don't have one, you just have some stars. So naturally it's wrong.


    Quzah.
    Last edited by quzah; 05-30-2010 at 06:16 PM.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. sorting number
    By Leslie in forum C Programming
    Replies: 8
    Last Post: 05-20-2009, 05:23 AM
  2. <Gulp>
    By kryptkat in forum Windows Programming
    Replies: 7
    Last Post: 01-14-2006, 01:03 PM
  3. Function Pointer help
    By Skydt in forum C Programming
    Replies: 5
    Last Post: 12-02-2005, 09:13 AM
  4. pointers
    By InvariantLoop in forum C Programming
    Replies: 13
    Last Post: 02-04-2005, 09:32 AM
  5. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 06:06 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21