qsort

Discussion in 'C Programming' started by Bill Cunningham, Nov 11, 2011.

  1. 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
    Bill Cunningham, Nov 11, 2011
    #1
    1. Advertising

  2. On Nov 11, 4:00 pm, "Bill Cunningham" <> 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);
    Malcolm McLean, Nov 11, 2011
    #2
    1. Advertising

  3. Bill Cunningham

    John Gordon Guest

    In <4ebd2a87$0$19698$> "Bill Cunningham" <> 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
    B is for Basil, assaulted by bears
    -- Edward Gorey, "The Gashlycrumb Tinies"
    John Gordon, Nov 11, 2011
    #3
  4. Malcolm McLean <> writes:

    > On Nov 11, 4:00 pm, "Bill Cunningham" <> 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.
    Ben Bacarisse, Nov 11, 2011
    #4
  5. 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
    Bill Cunningham, Nov 11, 2011
    #5
  6. On Saturday, November 12, 2011 12:39:49 AM UTC+8, Ben Bacarisse wrote:
    > Malcolm McLean <> writes:
    >
    > > On Nov 11, 4:00 pm, "Bill Cunningham" <> 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.
    88888 Dihedral, Nov 12, 2011
    #6
  7. 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
    Bill Cunningham, Nov 13, 2011
    #7
  8. "Bill Cunningham" <> 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) <http://www.ghoti.net/~kst>
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Nov 13, 2011
    #8
  9. Keith Thompson wrote:
    > "Bill Cunningham" <> 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
    Bill Cunningham, Nov 13, 2011
    #9
  10. Bill Cunningham

    Ian Collins Guest

    On 11/13/11 07:39 PM, Bill Cunningham wrote:
    > Keith Thompson wrote:
    >> "Bill Cunningham"<> 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
    Ian Collins, Nov 13, 2011
    #10
  11. Bill Cunningham

    Phil Carmody Guest

    Ian Collins <> writes:
    > On 11/13/11 07:39 PM, Bill Cunningham wrote:
    > > Keith Thompson wrote:
    > >> "Bill Cunningham"<> 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"?


    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
    Phil Carmody, Nov 14, 2011
    #11
  12. On Nov 13, 6:51 am, Ian Collins <> wrote:
    > On 11/13/11 07:39 PM, Bill Cunningham wrote:
    >
    > > Keith Thompson wrote:
    > >> "Bill Cunningham"<>  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"?


    does it normally include a "?"?
    Nick Keighley, Nov 14, 2011
    #12
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. richard

    Re: qsort and structs and ptrs

    richard, Aug 14, 2003, in forum: C Programming
    Replies:
    0
    Views:
    378
    richard
    Aug 14, 2003
  2. richard

    crashing qsort

    richard, Aug 14, 2003, in forum: C Programming
    Replies:
    34
    Views:
    1,321
  3. Debashish Chakravarty

    [possibly OT] the comparison function in qsort

    Debashish Chakravarty, Nov 23, 2003, in forum: C Programming
    Replies:
    0
    Views:
    300
    Debashish Chakravarty
    Nov 23, 2003
  4. Ingo Brueckl
    Replies:
    5
    Views:
    473
    Keith Thompson
    Dec 19, 2003
  5. Charlie Zender

    Need warning-free qsort() comparison functions

    Charlie Zender, Jan 1, 2004, in forum: C Programming
    Replies:
    21
    Views:
    1,234
Loading...

Share This Page