Mail Archives: djgpp-workers/2003/04/18/08:48:01
> > static int cmp (const void *e1, const void *e2)
> > {
> > return strcmp(*(const char **)e1, *(const char **)e2);
> > }
> >
> > Any suggestions on how to juggle consts or whatever to make
> it happy?
>
> This seems so obvious, but what have I missed :-
> return strcmp((const char *)e1, (const char *)e2);
Errm, no.
consider:
char* mystring1 = "abc";
char* mystring2 = "def";
cmp(&mystring1, &mystring2);
This is the usage suggested by the implementation of cmp above,
i.e. that its void* args are really pointers to char*. Those are
then dereferenced to get char*s that strcmp can use.
Using
strcmp((const char *)e1, (const char *)e2)
merely casts '&mystring1' to 'const char*' which is a no-no.
The solution is to add another const, I would expect. The
arguments are pointers to something that is const; 'consr char**'
is a pointer to a non-const pointer that points to something
const. Try casting to 'const char* const*' instead.
- Raw text -