473,466 Members | 1,508 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

Function pointer cast

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

Jun 27 '08 #1
3 2068
"Bartc" <bc@freeuk.comwrote in message
news:nh******************@text.news.virginmedia.co m...
>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]
)
According to the C99 standard, it may be possible as a common extension:
"J.5.7 Function pointer casts

1 A pointer to an object or to void may be cast to a pointer to a function,
allowing data to

be invoked as a function (6.5.4).

2 A pointer to a function may be cast to a pointer to an object or to void,
allowing a function to be inspected or modified (for example, by a debugger)
(6.5.4)."

Show the actual code you are trying to use.


** Posted from http://www.teranews.com **
Jun 27 '08 #2
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.

(It should compile to something like this in x86 code:
mov esi,[p]
call [esi]
)
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):

typedef void (*VoidOfVoid)(void);
((VoidOfVoid)*p)();

Be aware that treating an int value as a function pointer
is not something the Standard says must work. It is quite
likely to fail spectacularly in a 64-bit setting ...

--
Er*********@sun.com
Jun 27 '08 #3

"Eric Sosman" <Er*********@sun.comwrote 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
Jun 27 '08 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Bren | last post by:
Hi all, Given this situation: class Base { typedef void (Base::*FN_FOO)(); virtual void Foo() = 0; // pure virtual void GetFoo(FN_FOO pfnFoo) = 0; // pure virtual
7
by: Alfonso Morra | last post by:
How can this work ? I have the following declarations in a header: Header ========= typedef void (*VFPTR)(void) ; void FOO_Callback(void*, char*, char*, int, unsigned long, void*,void*);...
12
by: srinivas.satish | last post by:
Hi, is it possible to typecast a function pointer to two different prototypes. eg., typedef void (functptr1 *) (int , int); typedef void (functptr2 *) (int); functptr1 fptr; fptr =...
3
by: Beta What | last post by:
Hello, I have a question about casting a function pointer. Say I want to make a generic module (say some ADT implementation) that requires a function pointer from the 'actual/other modules'...
11
by: Felix Kater | last post by:
Hi, I can compile and run this code (see below) which twice calls the function f, first with too less, second with too much arguments. But is it legal and free of memory leaks and other...
6
by: Kiran | last post by:
Hi all, What I am trying to do is to pass a pointer to the first element of an array to a function, modify it in that function, and then print out the values of the array (which has been modified...
32
by: copx | last post by:
Why doesn't the C standard include generic function pointers? I use function pointers a lot and the lack of generic ones is not so cool. There is a common compiler extension (supported by GCC...
20
by: MikeC | last post by:
Folks, I've been playing with C programs for 25 years (not professionally - self-taught), and although I've used function pointers before, I've never got my head around them enough to be able to...
7
by: ghulands | last post by:
I am having trouble implementing some function pointer stuff in c++ An object can register itself for many events void addEventListener(CFObject *target, CFEventHandler callback, uint8_t...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.