Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

qsort

26 views
Skip to first unread message

Bill Cunningham

unread,
Nov 11, 2011, 9:00:25 AM11/11/11
to
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


Malcolm McLean

unread,
Nov 11, 2011, 9:56:39 AM11/11/11
to
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);


John Gordon

unread,
Nov 11, 2011, 10:20:11 AM11/11/11
to
In <4ebd2a87$0$19698$bbae...@news.suddenlink.net> "Bill Cunningham" <nos...@nspam.invalid> 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
gor...@panix.com B is for Basil, assaulted by bears
-- Edward Gorey, "The Gashlycrumb Tinies"

Ben Bacarisse

unread,
Nov 11, 2011, 11:39:49 AM11/11/11
to
Malcolm McLean <malcolm...@btinternet.com> writes:

> On Nov 11, 4:00 pm, "Bill Cunningham" <nos...@nspam.invalid> 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.

Bill Cunningham

unread,
Nov 11, 2011, 2:22:33 PM11/11/11
to
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


pete

unread,
Nov 11, 2011, 8:27:22 PM11/11/11
to
Bill Cunningham wrote:

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

/* BEGIN output from new.c */

From highest to lowest:
24.450000
24.000000
21.000000

/* END output from new.c */



/* BEGIN new.c */

#include <stdio.h>
#include <stdlib.h>

int
price_comp(const void *a, const void *b)
{
const double *aa = a;
const double *bb = b;

return *aa > *bb ? -1 : *aa != *bb;
}

int
main(void)
{
double prices[] = {24.45, 24, 21};
double *ptr = prices;
size_t n = sizeof prices / sizeof *prices;

qsort(prices, n, sizeof *prices, price_comp);

puts("\n/* BEGIN output from new.c */\n");
puts("From highest to lowest:");
while (n-- != 0) {
printf("%f\n", *ptr);
++ptr;
}
puts("\n/* END output from new.c */");
return 0;
}

/* END new.c */

--
pete

88888 Dihedral

unread,
Nov 12, 2011, 1:19:31 PM11/12/11
to
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.

Bill Cunningham

unread,
Nov 12, 2011, 7:10:21 PM11/12/11
to
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


Keith Thompson

unread,
Nov 12, 2011, 7:40:30 PM11/12/11
to
What for loop?

--
Keith Thompson (The_Other_Keith) ks...@mib.org <http://www.ghoti.net/~kst>
"We must do something. This is something. Therefore, we must do this."
-- Antony Jay and Jonathan Lynn, "Yes Minister"

pete

unread,
Nov 12, 2011, 10:42:11 PM11/12/11
to
if (*aa > *bb) {
return -1;
} else {
if (*aa != *bb) {
return 1;
} else {
return 0;
}
}

--
pete

Bill Cunningham

unread,
Nov 13, 2011, 1:39:42 AM11/13/11
to
Keith Thompson wrote:
> "Bill Cunningham" <nos...@nspam.invalid> 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


Ian Collins

unread,
Nov 13, 2011, 1:51:16 AM11/13/11
to
Doesn't a for loop normally include the word "for"?

--
Ian Collins

Phil Carmody

unread,
Nov 13, 2011, 7:14:38 PM11/13/11
to
Ok I saw the From: line and thought it might be part of a troll.

Phil
--
Unix is simple. It just takes a genius to understand its simplicity
-- Dennis Ritchie (1941-2011), Unix Co-Creator

Nick Keighley

unread,
Nov 14, 2011, 3:43:59 AM11/14/11
to
does it normally include a "?"?
0 new messages