void * compatibility

? Polfer polfer at b11.ingr.com
Thu Sep 20 04:41:07 AEST 1990


While porting some code to an ANSI compiler (NOT porting to ANSI), I received 
an error with code similar to the following:

    int (**command)();

    ...

    *command = NULL;

The warning was:

    "Type `void *' is not assignment compatible with type `int (*)()'."

The compiler is not in full ANSI mode (we must keep using a non ANSI compiler
on another platform), so prototyping is NOT being used.  It turns out that 
Metaware High C defines NULL in stdio.h as "(void *)0".  This gives a little 
bit more error checking than just using "0".  My question is why the above 
code errors out.  "*command" is a pointer to a function which returns int, but
it is still a pointer.  I understand that ANSI does not guarantee that
pointers to different types will have the same internal repesentations, but
isn't a "void *" supposed to be assignment compatible with ALL pointers?  

The fix is to use "*command = (int (*)())0;" or just plain "*command = 0;",
neither of which yield errors.  Could this be because "0" is considered the
generic NULL pointer value while the explicit cast (void *) fouls the 
assignment?  Any ideas would be appreciated!

-----

Dan Polfer             uunet!ingr!b11!dap!dan  (UUCP)
                       b11!dap!dan at ingr.com    (Internet)



More information about the Comp.lang.c mailing list