Re: typedef function with void parameters



On Thu, 13 Dec 2007 15:24:38 +0000, Ben Bacarisse
<ben.usenet@xxxxxxxxx> wrote:

William Xu <william.xwl@xxxxxxxxx> writes:

| int foo(char *p){}
| int bar(int *p){}
|
| typedef int (*FUNC)(void *);
|
| FUNC f1 = foo, f2 = bar;

So, this is not allowed?

Allowed is vague. It is a constraint violation and thus requires a
diagnostic (but that could be "I will convert these function pointers
for you" or it might be "Yuck! -- compilation halted").

Right. Although I am a bit surprised that the OP's version of gcc
defaults the diagnostic to an error; mine make it a warning.

If you convert (with a cast) it will work on some systems but it is
not guaranteed -- your code could break with a compiler update or even
a change in optimisation level. If you are prepared to take a chance,
always make sure that the call is though a pointer whose type matches
the type of function being called.

_If_ you convert back to the correct (func pointer) type before
calling, it IS guaranteed to work. (Converting to and) calling through
the wrong pointer pointer is not guaranteed, and indeed could break,
although I'd be surprised if optimization does so -- C is usually and
traditionally implemented with separate compilation and linking, for
which optimization that doesn't eliminate a call entirely (inlining)
can usually change calling conventions only in very limited ways.

- formerly david.thompson1 || achar(64) || worldnet.att.net
.



Relevant Pages