Function-types: compatability, and typedefs

Alan J Rosenthal flaps at dgp.toronto.edu
Sat Sep 2 01:34:40 AEST 1989


ok at cs.mu.oz.au (Richard O'Keefe) writes:
>If you want to sort an array of strings, e.g.
>	char *myarray[N_Of_Strings];
>	...
>	qsort(myarray, N_Of_Strings, sizeof myarray[0], {{SOMETHING}})
>**don't** try to pass 'strcmp' as the fourth argument of qsort()...
>What you need is
>    int strptrcmp(char **s1, char **s2)
>	{ return strcmp(*s1, *s2); }

Actually, you should write
    int strptrcmp(void *s1, void *s2)
    {
	return(strcmp(*(char **)s1, *(char **)s2));
    }

The original form will only work when the representation of (char **) is the
same as the representation of (void *), something not guaranteed by ansi C and
in fact not true on all machines (albeit true on most).

ajr



More information about the Comp.lang.c mailing list