Re: Cannot understand the following codes
- From: Joe Wright <joewwright@xxxxxxxxxxx>
- Date: Wed, 29 Mar 2006 14:10:05 -0500
Ben C wrote:
On 2006-03-28, Alf P. Steinbach <alfps@xxxxxxxx> wrote:* Ben C:On 2006-03-28, Alf P. Steinbach <alfps@xxxxxxxx> wrote:It will be OK, /if/ the type is some pointer to function (but not if the type is simply void*, in which case it's UB); I was too hasty there.It's probably a void(*)(). But not matter what type it has, the cast is undefined behavior in C++, and I believe also in C.Why's it undefined behaviour? I can see that it invites undefined
behaviour, but not that it entails it-- if the value of
stGOFuncs.p_go_Measurements is a function of the correct type won't it
be OK?
You're right, void * and function pointers are not compatible. I tried
this:
#include <stdio.h>
int f(int x)
{
return x * 2;
}
int main(void)
{
int (*q)(int) = f;
void *p = q; /* WARNING */
int (*r)(int) = p; /* WARNING */
int x = (*r)(3);
printf("Answer is %d\n", x);
return 0;
}
I had assumed void * was good for anything. But it's only good for data,
not functions.
I suppose on some machines functions might have a completely different
address space, be a completely different size etc., and the standard is
allowing for that...
Sorry about that... ;-)
Not at all, thank you, I was missing something.
Pedantically void pointers and function pointers are not compatible. But is a less pedantic world it might work.. if you change your line
int x = (*r)(3);
to
int x = (r)(3); or even
int x = r(3);
--
Joe Wright
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
.
- Follow-Ups:
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- References:
- Cannot understand the following codes
- From: gpsabove
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Re: Cannot understand the following codes
- From: Ben C
- Re: Cannot understand the following codes
- From: Alf P. Steinbach
- Re: Cannot understand the following codes
- From: Ben C
- Cannot understand the following codes
- Prev by Date: Re: Transfer data between processes
- Next by Date: Re: Cannot understand the following codes
- Previous by thread: Re: Cannot understand the following codes
- Next by thread: Re: Cannot understand the following codes
- Index(es):
Relevant Pages
|