Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| Message-ID | <500B7D7F.7CA2@mindspring.com> (permalink) |
|---|---|
| Date | 2012-07-22 00:11 -0400 |
| From | pete <pfiland@mindspring.com> |
| Organization | PF |
| 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> |
Ben Bacarisse wrote:
>
> Bill Cunningham <billcun@suddenlink.net> 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 <stdio.h>
> > #include <stdlib.h>
> > #include <string.h>
> > 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
Back to comp.lang.c | Previous | Next — Previous in thread | Next in thread | Find similar | Unroll thread
qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 14:14 -0700
Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-21 22:57 +0100
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-21 18:07 -0700
Re: qsort man page Mark Storkamp <mstorkamp@yahoo.com> - 2012-07-23 08:49 -0500
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 10:44 -0700
Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:02 -0700
Re: qsort man page John Gordon <gordon@panix.com> - 2012-07-26 16:41 +0000
Re: qsort man page tom st denis <tom@iahu.ca> - 2012-07-26 09:59 -0700
Re: qsort man page Ike Naar <ike@sverige.freeshell.org> - 2012-07-23 14:43 +0000
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 13:16 -0700
Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-23 13:29 -0700
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-23 16:56 -0700
Re: qsort man page Noob <root@localhost> - 2012-07-24 09:29 +0200
Re: qsort man page Keith Thompson <kst-u@mib.org> - 2012-07-24 23:17 -0700
Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-25 09:51 -0700
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-25 20:51 -0700
Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-26 14:27 -0700
Re: qsort man page Bill Cunningham <billcun@suddenlink.net> - 2012-07-26 18:20 -0700
Re: qsort man page Barry Schwarz <schwarz45@yahoo.com> - 2012-07-28 07:24 -0700
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:39 -0400
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 00:11 -0400
Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 14:41 +0100
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-22 13:01 -0400
Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-22 21:36 +0100
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 22:40 -0400
Re: qsort man page pacman@kosh.dhis.org (Alan Curry) - 2012-07-24 04:29 +0000
Re: qsort man page Ben Bacarisse <ben.usenet@bsb.me.uk> - 2012-07-24 11:40 +0100
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 22:06 -0400
Re: qsort man page Tim Rentsch <txr@alumni.caltech.edu> - 2012-09-07 11:38 -0700
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-23 23:34 -0400
Re: qsort man page pete <pfiland@mindspring.com> - 2012-07-24 00:07 -0400
Re: qsort man page "christian.bau" <christian.bau@cbau.wanadoo.co.uk> - 2012-07-24 16:36 -0700
csiph-web