Function pointer cast

B

Bartc

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
 
B

Bartc

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
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads


Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top