Re: Problem with pointer to function - what's wrong here?
- From: Ben Pfaff <blp@xxxxxxxxxxxxxxx>
- Date: Wed, 31 Jan 2007 13:39:00 -0800
Charles Sullivan <cwsulliv@xxxxxxxxxxxx> writes:
int (*myfunc)();
[...]
I've always believed this was legal code, and have used something
similar a few times in the past, but now when (<argument list>) is:
(unsigned char x1, unsigned char x2, unsigned char x3,
int *y1, unsigned int *y2, int *y3)
I get the compiler message:
"warning: initialization from incompatible pointer type"
The problem is that there is no way to call a function with that
prototype, through a function pointer without a prototype,
without invoking undefined behavior. The reason is that, when
you invoke a function for which no prototype is available, the
integer promotions are performed on each argument (and arguments
of type float are converted to double). Thus, an "unsigned char"
argument will be converted to and passed as int (or unsigned int
on unusual implementations).
This rule for compatibility of function types is specified, in
C99, in section 6.7.5.3 paragraph 15:
For two function types to be compatible, both shall specify
compatible return types.125) Moreover, ... [if] one type
has a parameter type list and the other type is specified
by a function declarator that is not part of a function
definition and that contains an empty identifier list, the
parameter list shall not have an ellipsis terminator and
the type of each parameter shall be compatible with the
type that results from the application of the default
argument promotions.
It's a rather opaque paragraph, but I'm pretty sure that's what
it means.
--
int main(void){char p[]="ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz.\
\n",*q="kl BIcNBFr.NKEzjwCIxNJC";int i=sizeof p/2;char *strchr();int putchar(\
);while(*q){i+=strchr(p,*q++)-p;if(i>=(int)sizeof p)i-=sizeof p-1;putchar(p[i]\
);}return 0;}
.
- References:
- Problem with pointer to function - what's wrong here?
- From: Charles Sullivan
- Problem with pointer to function - what's wrong here?
- Prev by Date: Problem with pointer to function - what's wrong here?
- Next by Date: Re: Problem with pointer to function - what's wrong here?
- Previous by thread: Problem with pointer to function - what's wrong here?
- Next by thread: Re: Problem with pointer to function - what's wrong here?
- Index(es):
Relevant Pages
|