Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!border3.nntp.dca.giganews.com!Xl.tags.giganews.com!border1.nntp.dca.giganews.com!nntp.giganews.com!local2.nntp.dca.giganews.com!nntp.earthlink.com!news.earthlink.com.POSTED!not-for-mail NNTP-Posting-Date: Sun, 22 Jul 2012 12:01:42 -0500 Message-ID: <500C31F3.518B@mindspring.com> Date: Sun, 22 Jul 2012 13:01:39 -0400 From: pete Reply-To: pfiland@mindspring.com Organization: PF X-Mailer: Mozilla 3.04Gold (WinNT; I) MIME-Version: 1.0 Newsgroups: comp.lang.c Subject: Re: qsort man page References: <89644623-7e1c-4f8b-9f4d-6443526f0f73@u2g2000yqj.googlegroups.com> <0.7b858dd95d3281469ee0.20120721225709BST.87boj893re.fsf@bsb.me.uk> <500B7D7F.7CA2@mindspring.com> <0.58ba0ee876bab752444c.20120722144128BST.871uk37w1j.fsf@bsb.me.uk> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 69 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.220.98 X-Trace: sv3-PbFbjf/wCk0F259J5ukbLyO2Rmh/PS2f+DRZFO1anmKSuD97A2PbmAhfQL1lq5CtQ5exPC5zoLCVN3H!cJ188buye2fHWYuFV+2oDRf+zFMOPciyXP5wZl9lz48EVaJy4/fbWjCejMCDlAMhpB7tShWyEAf9!QqxaSivi7Q== X-Abuse-and-DMCA-Info: Please be sure to forward a copy of ALL headers X-Abuse-and-DMCA-Info: Otherwise we will be unable to process your complaint properly X-Postfilter: 1.3.40 X-Original-Bytes: 3642 Xref: csiph.com comp.lang.c:23581 Ben Bacarisse wrote: > > pete writes: > > > Ben Bacarisse wrote: > >> > >> Bill Cunningham writes: > > >> > #include > >> > #include > >> > #include > >> > static int > >> > cmpstringp(const void *p1, const void *p2) > >> > { > >> > /* The actual arguments to this function are "pointers to > >> > pointers to char", but strcmp(3) arguments are "pointers > >> > to char", hence the following cast plus dereference */ > >> > return strcmp(* (char * const *) p1, * (char * const *) p2); > >> > } > > > > I think that the way that the const keyword > > is used in the return statement, > > shows some confusion on the part of the author. > > > > It causes the result of the dereference > > in the strcmp function call, to be const qualified, > > which is neither required nor consistent > > with the parameters of strcmp > > being pointers to const qualified types. > > I am not so sure. The const that is there is a Good Const. > Omitting it might cause a warning > (gcc will oblige) about "casting away" const. > The question as to whether there should be another: > > *(const char *const *)p1 > > is not clear cut to me. > The cast, as written, is a correct reflection > of the object being sorted, > and there is some documentary merit in doing that > -- cast only to the type of the thing being sorted. There is no const in the type of the data being sorted. The array being sorted by cmpstringp in the example, is argv, which is an array of (char *) type elements. The const is introduced by the parameter type of cmpstringp. > Your suggestion, to cast to the type needed by strcmp, > is not wrong but, in general, I would avoid it. > If you cast to the type of the actual data, > the compiler will warn you if this type is not suitable for the > call being made. In essence, you get a little more checking. > No big deal in this case because strcmp takes "safer" arguments that > those being passed, but if I were forced to used some odd compare > function like > > int caseless_compare_that_alters_the_data(char *s1, char *s2); > > to sort an array of const char **, > my rule would show up a problem. > You cast to the type of the actual data, > and the call to this function will be flagged up as dangerous. -- pete