At 6:57 on 02/23/2002 CST, Brian Lee Ray <[EMAIL PROTECTED]> wrote:

> However, a null pointer constant, expressed as either 0 or (void*)0 (and the
> definition of NULL must be one of the above), can be safely compared with 
> or assigned to either a function pointer or a data pointer. Consider the foll
owing:
> 
> void *v=NULL;
> typedef void(*fptr)(void);
> fptr f;
> 
> 
> 1:f=v; /*invalid: assignment of a void pointer to a function pointer*/

"test2.c", line 9: Error:
  [ISO 6.3.4]: Can't convert function pointer 'void *' to non-function pointer 'void ( 
* ) ( void )'.
  [ISO 6.3.16]: Can't perform this conversion by assignment.


> 2:f=0;
> f=NULL;
> f=(void*)0; /*all valid: assignment of a null pointer
>                           *constant to a function pointer*/

It doesn't accept that last one.

"test2.c", line 12: Error:
  [ISO 6.3.4]: Can't convert function pointer 'void *' to non-function pointer 'void ( 
* ) ( void )'.
  [ISO 6.3.16]: Can't perform this conversion by assignment.


> 3:v=f; /*invalid: assignment of a function pointer to a void pointer*/

"test2.c", line 14: Error:
  [ISO 6.3.4]: Can't convert function pointer 'void ( * ) ( void )' to non-function 
pointer 'void *'.
  [ISO 6.3.16]: Can't perform this conversion by assignment.

> 4:f==0;/*valid:comparison of a function pointer to a null pointer constant*/
> 5:f==(void*)0;/*valid:comparison of a function pointer to a null pointer cons
tant*/

"test2.c", line 16: Error:
  [ISO 6.3.4]: Can't convert function pointer 'void ( * ) ( void )' to non-function 
pointer 'void *'.
  [ISO 6.3]: Conversion of operand 1 in operation '=='.


> 6:f==NULL;/*valid:comparison of a function pointer to a null pointer constant
*/
> 7:f==(fptr)v;/*must compile, however, unless both f and v are null pointers,
>               *the result is undefined. I would expect a warning for this, as
>               *it is a really stupid thing to do. after all, if you know that
>               *f and v are both null pointers, the result will hardly be surp
rising*/

"test2.c", line 18: Error:
  [ISO 6.3.4]: Can't convert function pointer 'void *' to non-function pointer 'void ( 
* ) ( void )'.
  [ISO 6.3.4]: Can't perform this conversion using an explicit cast.


> > Function pointers are not data pointers, compilers are allowed to warn.
> Yes, a compiler can warn about anything at any time according to the
> ANSI standard. However, for a strictly conforming program, it must produce
> an executable image which produces the expected output, therefore it is my
> contention that if the following program:
> 
> -----begin test.c------
> #include <stdio.h>
> int main(void)
> {
>     void(*f)(void);
>     f=NULL;
>     if(f==NULL)
>         puts("skippy");
>     return 0;
> }
> ------end test.c-------
> does not both compile and place the string "skippy" on stdout (followed by a
> newline) on tcc, then tcc is not a conforming C implementation.

That does compile.  But that's because tcc is somehow defining NULL as 0, 
rather than ((void*)0), which is what it should be according to stdlib.h.
(confirmed with tcc -E).

It does give a warning with LCC though:

   test.c:5: warning: conversion from `pointer to void' to `pointer to void 
function(void)' is compiler dependent


--Josh


Reply via email to