Function pointer cast

Discussion in 'C Programming' started by Bartc, Apr 17, 2008.

  1. Bartc

    Bartc Guest

    I have a variable like this:

    int *p;

    Sometimes this points to an int location containing the address of a
    void(void) function.

    How can I cast it so that I can call that function?

    I've tried casting to void **(void). The compiler keeps saying such a cast
    is illegal.

    (It should compile to something like this in x86 code:
    mov esi,[p]
    call [esi]
    )

    Thanks,

    -- Bartc
     
    Bartc, Apr 17, 2008
    #1
    1. Advertising

  2. Bartc

    Bartc Guest

    "Eric Sosman" <> wrote in message
    news:1208464501.259090@news1nwk...
    > Bartc wrote:
    >> I have a variable like this:
    >>
    >> int *p;
    >>
    >> Sometimes this points to an int location containing the address of a
    >> void(void) function.
    >>
    >> How can I cast it so that I can call that function?
    >>
    >> I've tried casting to void **(void). The compiler keeps saying such a
    >> cast is illegal.


    > The call would look like
    >
    > ((void(*)(void))*p)();
    >
    > ... which might be more readable with a typedef (I dislike
    > typedefs that name object pointer types, but for function
    > pointers they are often helpful):


    That works perfectly, thanks!

    I don't care what it looks like, it's only used in one place; in context it
    looks like this (part of byte-code opcode dispatcher):

    stopped=0;
    do {
    ((void(*)(void))*pcptr)();
    } while (!stopped);

    -- Bartc
     
    Bartc, Apr 17, 2008
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tirath
    Replies:
    3
    Views:
    734
    Ivan Vecerina
    Oct 12, 2003
  2. MSG

    to cast or not to cast malloc ?

    MSG, Feb 6, 2004, in forum: C Programming
    Replies:
    38
    Views:
    1,117
    Dan Pop
    Feb 10, 2004
  3. Szabolcs Borsanyi
    Replies:
    6
    Views:
    686
    Ben Bacarisse
    May 23, 2008
  4. Replies:
    1
    Views:
    349
  5. Hansen
    Replies:
    3
    Views:
    1,155
    rep_movsd
    Apr 24, 2010
Loading...

Share This Page