delorie.com/archives/browse.cgi   search  
Mail Archives: djgpp-workers/2003/04/18/08:48:01

From: "Tim Van Holder" <tim DOT van DOT holder AT pandora DOT be>
To: <djgpp-workers AT delorie DOT com>, "'Charles Sandmann'" <sandmann AT clio DOT rice DOT edu>
Cc: <dborca AT yahoo DOT com>
Subject: Re: dxe review
Date: Fri, 18 Apr 2003 14:13:21 +0200
MIME-Version: 1.0
X-Mailer: Microsoft Outlook, Build 11.0.4920
In-Reply-To: <002701c30596$155c7540$0100a8c0@acp42g>
X-MimeOLE: Produced By Microsoft MimeOLE V6.00.3718.0
Thread-Index: AcMFljgLmJC4PdGCREq3lqvQG9hFfQADBwXA
Message-Id: <20030418121357.B894A1C00AF@iceage.anubex.com>
Reply-To: djgpp-workers AT delorie DOT com
Errors-To: nobody AT delorie DOT com
X-Mailing-List: djgpp-workers AT delorie DOT com
X-Unsubscribes-To: listserv AT delorie DOT com

> > 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 -


  webmaster     delorie software   privacy  
  Copyright © 2019   by DJ Delorie     Updated Jul 2019