Mail Archives: djgpp/1998/06/22/04:10:58
Kaz Kylheku wrote:
> You can create a typedef name for the function pointer and then use
> the parenthesized typedef name. Or you can write cast expressions
> like:
>
> void *q = 0;
> double (*p)(double, int) = (double (*)(double, int)) q;
I think the original poster was really looking for the answer to the
question: For a given declaration, how do I determine its type (for
casting)?
The answer is that if you remove the identifier name from the
declaration, that's the type. So in the declaration int p, int is the
type; in the declaration char *s, char * is the type, and in the
declaration double (*p)(double, int), double (*)(double, int) is the
type, even though it looks a little strange.
> Note that conversion between pointers to void and function pointers
> is not a feature of the C language; it is merely a common extension.
Are you sure about that? (I couldn't find a reference confirming that
it's possible, and if there isn't one, then, well, it isn't.)
In any case, if all the original poster wanted was a "generic function
pointer" (which is how void * is being used in this case), then they can
just use some generic function pointer type -- say, int (*)(void) -- for
the conversions, and then back to the original function pointer type
before the call is made, since this guaranteed by ANSI 6.3.4.
> Yes; pointers to void may only be implicitly converted to object
> pointers
> or pointers to incomplete type. Converting a function point to void *
> and vice versa requires a cast operator.
I believe implicit casts to void * (but not from void * to something
else) are perfectly legal. i.e.,
int i;
void *p = &i; /* not an error */
Certainly, explicitly casting to void * is never a bad idea. The
standard is somewhat opaque on this subject, though, so I'm not entirely
sure (ANSI 6.3.16.1)
--
Erik Max Francis, &tSftDotIotE / mailto:max AT alcyone DOT com
Alcyone Systems / http://www.alcyone.com/max/
San Jose, California, United States / icbm:+37.20.07/-121.53.38
\
Regret it? nope. / Said it? yep.
/ Ice Cube
- Raw text -