Path: csiph.com!v102.xanadu-bbs.net!xanadu-bbs.net!news.glorb.com!npeer01.iad.highwinds-media.com!news.highwinds-media.com!feed-me.highwinds-media.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: Sat, 21 Jul 2012 23:11:46 -0500 Message-ID: <500B7D7F.7CA2@mindspring.com> Date: Sun, 22 Jul 2012 00:11:43 -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> Content-Type: text/plain; charset=us-ascii Content-Transfer-Encoding: 7bit Lines: 55 X-Usenet-Provider: http://www.giganews.com NNTP-Posting-Host: 4.154.216.30 X-Trace: sv3-oGzsestMzRj4zaoANL/cIDJgIaXC7W6mmnwamE6dcj7vYnfKPohlggOYy1yy1AJIXlB+R8UHPTL+WVs!YPV0QZ0S2GTboWXa+DmWrq6HfFH6L63qDC2M0aHmcBDZEPZ0sduc0EUb2VliyCkytxZdpJrXYsbS!FQ3GHnotMA== 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: 3074 X-Received-Bytes: 3215 Xref: csiph.com comp.lang.c:23542 Ben Bacarisse wrote: > > Bill Cunningham writes: > > > This is from the qsort man page. > > I don't quite understand why there is > > a comment here saying that arguments to this generic function > > to pass to qsort is a pointer to pointer. > > It looks to me like cmpstringp takes > > a generic pointer. Not pointer to pointer. Why does the comment say > > pointer to pointer? > > Yup, cmpstringp takes two generic pointers, > but in order to do anything > useful with them it must know to what kind of data they really point. > If the array being sorted is an array of ints, > then the two pointers are > really pointing at int data, > so casting to int * and dereferencing would > be the right thing to do (you'd write *(const int *)p1). > > But, argv -- the array being sorted here -- is an array of character > pointers so the comparisson function gets passed pointers to a pair of > these character pointers, converted to "generic" (void *) pointers. > > > This is confusing me also with the strcmp > > parameters which are cast as a pointer to pointer and dereferenced. I > > hope I'm clear. THanks. > > > > Bill > > > > #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. -- pete