Function-types: compatability, and typedefs

Karl Heuer karl at haddock.ima.isc.com
Tue Sep 5 04:49:01 AEST 1989


ok at cs.mu.oz.au (Richard O'Keefe) writes:
>flaps at dgp.toronto.edu (Alan J Rosenthal) writes:
>>ok at cs.mu.oz.au (Richard O'Keefe) writes:
>>>int cmp(char **s1, char **s2) { return strcmp(*s1, *s2); }
>
>>Actually, you should write
>>int cmp(void *s1, void *s2) { return(strcmp(*(char **)s1, *(char **)s2)); }
>
>Sorry; I was working from an old [pre-ANSI] manual with char *base and so on.

That's not the reason for the correction.  The correct pre-ANSI code couldn't
use "void *" or a prototype, but you'd still have to declare the arguments to
be generic ("char *") and then cast them to "char **".

This isn't just a quibble.  There are many real machines out there that have
more than one pointer format.  The code will fail in mysterious ways if you
don't declare the formal arguments to be the same type as the actuals.

Karl W. Z. Heuer (ima!haddock!karl or karl at haddock.isc.com), The Walking Lint
("But I tried it on my VAX, and it worked, so it must be right!")



More information about the Comp.lang.c mailing list