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. Advertisements

  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. Advertisements

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. glen stark
    Replies:
    2
    Views:
    857
    Ron Natalie
    Oct 10, 2003
  2. tirath
    Replies:
    3
    Views:
    944
    Ivan Vecerina
    Oct 12, 2003
  3. Fraser Ross
    Replies:
    4
    Views:
    1,325
    Fraser Ross
    Aug 14, 2004
  4. murgan
    Replies:
    6
    Views:
    5,250
    Thad Smith
    Dec 21, 2005
  5. Vijai Kalyan
    Replies:
    4
    Views:
    1,078
    Vijai Kalyan
    Nov 8, 2005
  6. Szabolcs Borsanyi
    Replies:
    6
    Views:
    875
    Ben Bacarisse
    May 23, 2008
  7. Replies:
    1
    Views:
    457
  8. Hansen
    Replies:
    3
    Views:
    1,463
    rep_movsd
    Apr 24, 2010
Loading...