qsort

B

Bill Cunningham

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
 
M

Malcolm McLean

    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);
 
J

John Gordon

In said:
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?
 
B

Ben Bacarisse

Malcolm McLean said:
On Nov 11, 4:00 pm, "Bill Cunningham" <[email protected]> wrote:
double prices[]={24.45,24,21};
/* 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.]
 
B

Bill Cunningham

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
 
8

88888 Dihedral

Malcolm McLean said:
On Nov 11, 4:00 pm, "Bill Cunningham" <[email protected]> wrote:
double prices[]={24.45,24,21};
/* 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.]

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.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

Qsort() messing with my entire Code 0
Qsort() is messing with my entire Code!!! 0
qsort returning index 2
qsort man page 23
qsort semantics 10
Qsort-ing structures 16
calling qsort 59
How to apply qsort() with a parameter? 17

Members online

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top