Velocity Reviews - Computer Hardware Reviews

Velocity Reviews > Newsgroups > Programming > C Programming > qsort

Reply
Thread Tools

qsort

 
 
Bill Cunningham
Guest
Posts: n/a
 
      11-11-2011
Seeing that in that last paramter of qsort and int is returned I guess
you could pass NULL to that parameter. Would you want to do that. I have a
series of doubles...

double prices[]={24.45,24,21};

I want to sort them from highest to lowest. Still not knowing C's semantics
well enough there's probably a simpler way but what kind of function would I
want to write to pass to that last parameter of qsort?

Bill


 
Reply With Quote
 
 
 
 
Malcolm McLean
Guest
Posts: n/a
 
      11-11-2011
On Nov 11, 4:00*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:
> * * Seeing that in that last paramter of qsort and int is returned I guess
> you could pass NULL to that parameter. Would you want to do that. I have a
> series of doubles...
>
> double prices[]={24.45,24,21};
>
> I want to sort them from highest to lowest. Still not knowing C's semantics
> well enough there's probably a simpler way but what kind of function would I
> want to write to pass to that last parameter of qsort?
>
>

int compdoubles(const void *e1, const void *e2)
{
const double *d1 = e1;
const double *d2 = e2;

/* now you write the logic to compare the doubles, returning 0 if
they are equal, and -1 or +1 is they are unequal. Use *d1, *d2,
because you have pointers*/

}

/* in the main function */

qsort(prices, 3, sizeof(double), compdoubles);


 
Reply With Quote
 
 
 
 
John Gordon
Guest
Posts: n/a
 
      11-11-2011
In <4ebd2a87$0$19698$(E-Mail Removed)> "Bill Cunningham" <(E-Mail Removed)> writes:

> Seeing that in that last paramter of qsort and int is returned I guess
> you could pass NULL to that parameter. Would you want to do that.


No, you would not want to pass NULL. The purpose of that parameter is so
you can pass a pointer to a function that compares two of your data
elements. If you pass NULL, qsort has no way to compare the elements.

> ... what kind of function would I want to write to pass to that last
> parameter of qsort?


If you knew the parameter was for passing in a comparison function, why did
you ask about passing NULL?

--
John Gordon A is for Amy, who fell down the stairs
http://www.velocityreviews.com/forums/(E-Mail Removed) B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

 
Reply With Quote
 
Ben Bacarisse
Guest
Posts: n/a
 
      11-11-2011
Malcolm McLean <(E-Mail Removed)> writes:

> On Nov 11, 4:00*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:

<snip>
>> double prices[]={24.45,24,21};

<snip>
> /* in the main function */
>
> qsort(prices, 3, sizeof(double), compdoubles);


It's almost always better to write

qsort(prices, 3, sizeof *prices, compdoubles);

in these cases. When the type has been lost, you can't do this, of
course, but if you can, I can't see any reason not to.

The gain is small since you usually can't change the array type without
having to make other changes to the qsort call, but it still saves the
reader a millisecond or two of checking that the size, at least, is
right.

[In this case the 3 can also be replaced, but that it less common in
real code.]

--
Ben.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-11-2011
John Gordon wrote:

[snip]

> If you knew the parameter was for passing in a comparison function,
> why did
> you ask about passing NULL?


I really don't know anything about qsort. I know that NULL can be passed
in some cases to some function parameters. But as you say I guess this is
not one of those functions you'd ever want to do that with for *any* reason.

Bill


 
Reply With Quote
 
88888 Dihedral
Guest
Posts: n/a
 
      11-12-2011
On Saturday, November 12, 2011 12:39:49 AM UTC+8, Ben Bacarisse wrote:
> Malcolm McLean <(E-Mail Removed)> writes:
>
> > On Nov 11, 4:00*pm, "Bill Cunningham" <(E-Mail Removed)> wrote:

> <snip>
> >> double prices[]={24.45,24,21};

> <snip>
> > /* in the main function */
> >
> > qsort(prices, 3, sizeof(double), compdoubles);

>
> It's almost always better to write
>
> qsort(prices, 3, sizeof *prices, compdoubles);
>
> in these cases. When the type has been lost, you can't do this, of
> course, but if you can, I can't see any reason not to.
>
> The gain is small since you usually can't change the array type without
> having to make other changes to the qsort call, but it still saves the
> reader a millisecond or two of checking that the size, at least, is
> right.
>
> [In this case the 3 can also be replaced, but that it less common in
> real code.]
>
> --
> Ben.


OK, this is the famous function pointer that can be reloaded in C. The qsort accepts function pointers customized. Thus, it is easy to delegate a function.
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-13-2011
pete wrote:
> return *aa > *bb ? -1 : *aa != *bb;


Can you show me that for loop that's not in shorthand? This is kind of
hard to read.

Bill


 
Reply With Quote
 
Keith Thompson
Guest
Posts: n/a
 
      11-13-2011
"Bill Cunningham" <(E-Mail Removed)> writes:
> pete wrote:
>> return *aa > *bb ? -1 : *aa != *bb;

>
> Can you show me that for loop that's not in shorthand? This is kind of
> hard to read.


What for loop?

--
Keith Thompson (The_Other_Keith) (E-Mail Removed) <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"
 
Reply With Quote
 
Bill Cunningham
Guest
Posts: n/a
 
      11-13-2011
Keith Thompson wrote:
> "Bill Cunningham" <(E-Mail Removed)> writes:
>> pete wrote:
>>> return *aa > *bb ? -1 : *aa != *bb;

>>
>> Can you show me that for loop that's not in shorthand? This is
>> kind of hard to read.

>
> What for loop?


Ok I saw the ?: and thought it might be part of for.

Bill


 
Reply With Quote
 
Ian Collins
Guest
Posts: n/a
 
      11-13-2011
On 11/13/11 07:39 PM, Bill Cunningham wrote:
> Keith Thompson wrote:
>> "Bill Cunningham"<(E-Mail Removed)> writes:
>>> pete wrote:
>>>> return *aa> *bb ? -1 : *aa != *bb;
>>>
>>> Can you show me that for loop that's not in shorthand? This is
>>> kind of hard to read.

>>
>> What for loop?

>
> Ok I saw the ?: and thought it might be part of for.


Doesn't a for loop normally include the word "for"?

--
Ian Collins
 
Reply With Quote
 
 
 
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
Need warning-free qsort() comparison functions Charlie Zender C Programming 21 01-03-2004 12:32 PM
How to declare an array of sort functions for qsort()? Ingo Brueckl C Programming 5 12-19-2003 01:08 AM
[possibly OT] the comparison function in qsort Debashish Chakravarty C Programming 0 11-23-2003 04:59 AM
crashing qsort richard C Programming 34 08-28-2003 12:10 PM
Re: qsort and structs and ptrs richard C Programming 0 08-13-2003 11:09 PM



Advertisments