qsort problem

Discussion in 'C Programming' started by istillshine@gmail.com, Apr 6, 2008.

  1. Guest

    I called qsort (from standard library) somewhere in my function.

    qsort(temp, tree_num, sizeof(int), compare);

    I defined compare before the caller in the same .c file.

    int compare(const int *a, const int *b)
    {
    int val1, val2;

    val1 = *a;
    val2 = *b;

    return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
    }


    When compiling, I got this warning:

    passing arg 4 of `qsort' from incompatible pointer type

    I really could not find out why compare is incompatible.
    , Apr 6, 2008
    #1
    1. Advertising

  2. wrote:
    > I called qsort (from standard library) somewhere in my function.
    >
    > qsort(temp, tree_num, sizeof(int), compare);
    >
    > I defined compare before the caller in the same .c file.
    >
    > int compare(const int *a, const int *b)
    > {
    > int val1, val2;
    >
    > val1 = *a;
    > val2 = *b;
    >
    > return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
    > }
    >
    >
    > When compiling, I got this warning:
    >
    > passing arg 4 of `qsort' from incompatible pointer type
    >
    > I really could not find out why compare is incompatible.
    >



    It should be:

    int compare(const void *a, const void *b);
    Ioannis Vranos, Apr 6, 2008
    #2
    1. Advertising

  3. Guest

    On Apr 5, 9:37 pm, Ioannis Vranos <>
    wrote:
    >
    > It should be:
    >
    > int compare(const void *a, const void *b);


    But this causes another warning:

    dereferencing a void pointer on the line where "val1 = *a; " appears.
    , Apr 6, 2008
    #3
  4. wrote:
    > On Apr 5, 9:37 pm, Ioannis Vranos <>
    > wrote:
    >> It should be:
    >>
    >> int compare(const void *a, const void *b);

    >
    > But this causes another warning:
    >
    > dereferencing a void pointer on the line where "val1 = *a; " appears.



    int compare(const void *a, const void *b)
    {
    int *pval1= a, *val2= b;

    /* ... */
    }
    Ioannis Vranos, Apr 6, 2008
    #4
  5. Correction:


    Ioannis Vranos wrote:
    >


    int compare(const void *a, const void *b)
    {
    const int *pval1= a, *pval2= b;>
    /* ... */
    }
    Ioannis Vranos, Apr 6, 2008
    #5
  6. Correction, removed a typo.


    Ioannis Vranos wrote:
    > Correction:
    >
    >
    > Ioannis Vranos wrote:
    >
    > int compare(const void *a, const void *b)
    > {

    const int *pval1= a, *pval2= b;
    > /* ... */
    > }
    Ioannis Vranos, Apr 6, 2008
    #6
  7. On Sat, 5 Apr 2008 18:27:52 -0700 (PDT), wrote:

    >I called qsort (from standard library) somewhere in my function.
    >
    >qsort(temp, tree_num, sizeof(int), compare);
    >
    >I defined compare before the caller in the same .c file.
    >
    >int compare(const int *a, const int *b)
    >{
    > int val1, val2;
    >
    > val1 = *a;
    > val2 = *b;
    >
    > return (val1 > val2) ? -1 : ((val1 < val2) ? 1 : 0);
    >}
    >
    >
    >When compiling, I got this warning:
    >
    >passing arg 4 of `qsort' from incompatible pointer type
    >
    >I really could not find out why compare is incompatible.


    Look in your C reference. What is the only acceptable type for the
    fourth argument to qsort? What is the type of your fourth argument?
    Are they the same?


    Remove del for email
    Barry Schwarz, Apr 6, 2008
    #7
  8. Guest

    I wrote a compare function for qsort. I tested version 1 and version
    2 (below). Version 1 worked but version 2 did not work. Why?

    /*
    * Version 1
    * Compare two numbers, used in qsort.
    */
    int compare(const void *a, const void *b)
    {
    int *val1, *val2;

    val1 = (int *) a;
    val2 = (int *) b;

    return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    }


    /*
    * Version 2
    * Compare two numbers, used in qsort.
    */
    int compare(const void *a, const void *b)
    {
    int *val1 = (int *) a, *val2 = (int *) b;

    return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    }
    , Apr 13, 2008
    #8
  9. On Sun, 13 Apr 2008 05:10:15 -0700, istillshine wrote:
    > I wrote a compare function for qsort. I tested version 1 and version 2
    > (below). Version 1 worked but version 2 did not work. Why?


    Because either your compiler is broken, or you didn't post the real code.
    The two versions you posted are equivalent in every way that matters to
    qsort.

    That said,

    > int compare(const void *a, const void *b)


    So the parameters are pointers to a read-only type...

    > {
    > int *val1, *val2;
    >
    > val1 = (int *) a;
    > val2 = (int *) b;


    ....which you cast to pointers to a writeable type...

    > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);


    ....in order to only read the values. Why?

    > }
    Harald van Dijk, Apr 13, 2008
    #9
  10. wrote:
    > I wrote a compare function for qsort. I tested version 1 and version
    > 2 (below). Version 1 worked but version 2 did not work. Why?
    >
    > /*
    > * Version 1
    > * Compare two numbers, used in qsort.
    > */
    > int compare(const void *a, const void *b)
    > {
    > int *val1, *val2;
    >
    > val1 = (int *) a;
    > val2 = (int *) b;
    >
    > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    > }
    >
    >
    > /*
    > * Version 2
    > * Compare two numbers, used in qsort.
    > */
    > int compare(const void *a, const void *b)
    > {
    > int *val1 = (int *) a, *val2 = (int *) b;
    >
    > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    > }



    In both versions the function arguments are const void * and you assign
    them to int *, instead of const int *.
    Ioannis Vranos, Apr 13, 2008
    #10
  11. Guest

    On Apr 13, 8:31 am, Harald van D©¦k <> wrote:
    > On Sun, 13 Apr 2008 05:10:15 -0700, istillshine wrote:
    > > I wrote a compare function for qsort. I tested version 1 and version 2
    > > (below). Version 1 worked but version 2 did not work. Why?

    >
    > Because either your compiler is broken, or you didn't post the real code.
    > The two versions you posted are equivalent in every way that matters to
    > qsort.
    >
    > That said,
    >
    > > int compare(const void *a, const void *b)

    >
    > So the parameters are pointers to a read-only type...
    >
    > > {
    > > int *val1, *val2;

    >
    > > val1 = (int *) a;
    > > val2 = (int *) b;

    >
    > ...which you cast to pointers to a writeable type...
    >
    > > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);

    >
    > ...in order to only read the values. Why?
    >
    > > }


    Thanks. I rewrote it, as shown below.


    /*
    * Compare two numbers, used in qsort.
    */
    int compare(const void *a, const void *b)
    {
    const int *val1 = a, *val2 = b;

    return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    }
    , Apr 13, 2008
    #11
  12. Ioannis Vranos <> writes:
    > wrote:
    >> I wrote a compare function for qsort. I tested version 1 and version
    >> 2 (below). Version 1 worked but version 2 did not work. Why?
    >>
    >> /*
    >> * Version 1
    >> * Compare two numbers, used in qsort.
    >> */
    >> int compare(const void *a, const void *b)
    >> {
    >> int *val1, *val2;
    >>
    >> val1 = (int *) a;
    >> val2 = (int *) b;
    >>
    >> return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    >> }
    >>
    >>
    >> /*
    >> * Version 2
    >> * Compare two numbers, used in qsort.
    >> */
    >> int compare(const void *a, const void *b)
    >> {
    >> int *val1 = (int *) a, *val2 = (int *) b;
    >>
    >> return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    >> }

    >
    > In both versions the function arguments are const void * and you assign
    > them to int *, instead of const int *.


    Yes, and that should be fixed, but that's not going to cause any real
    problems in the behavior of qsort(). (Also, the casts aren't
    necessary.) As Harald pointed out, both versions appear to be
    equivalent.

    Also, the OP merely told us that version 2 "did not work". That's not
    enough information. *How* did it not work?

    --
    Keith Thompson (The_Other_Keith) <>
    Nokia
    "We must do something. This is something. Therefore, we must do this."
    -- Antony Jay and Jonathan Lynn, "Yes Minister"
    Keith Thompson, Apr 13, 2008
    #12
  13. On Sun, 13 Apr 2008 05:10:15 -0700 (PDT), wrote:

    >I wrote a compare function for qsort. I tested version 1 and version
    >2 (below). Version 1 worked but version 2 did not work. Why?
    >
    > /*
    > * Version 1
    > * Compare two numbers, used in qsort.
    > */
    >int compare(const void *a, const void *b)
    >{
    > int *val1, *val2;
    >
    > val1 = (int *) a;
    > val2 = (int *) b;
    >
    > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    >}
    >
    >
    >/*
    > * Version 2
    > * Compare two numbers, used in qsort.
    > */
    >int compare(const void *a, const void *b)
    >{
    > int *val1 = (int *) a, *val2 = (int *) b;
    >
    > return (*val1 > *val2) ? -1 : ((*val1 < *val2) ? 1 : 0);
    >}


    Since there are no significant differences between the two and neither
    has an obvious error, you need to provide us the calling function and
    the data you used to test. It would also be good if you told us in
    what way version 2 did not work.


    Remove del for email
    Barry Schwarz, Apr 14, 2008
    #13
    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. Excluded_Middle

    qsort + structure problem

    Excluded_Middle, Oct 26, 2004, in forum: C Programming
    Replies:
    7
    Views:
    433
    Old Wolf
    Nov 4, 2004
  2. Replies:
    16
    Views:
    522
    CBFalconer
    Dec 10, 2004
  3. c_programmer
    Replies:
    3
    Views:
    393
    Richard Bos
    Jun 9, 2005
  4. Replies:
    6
    Views:
    728
  5. Igal

    problem with qsort - Visual Studio

    Igal, May 21, 2008, in forum: C Programming
    Replies:
    0
    Views:
    539
Loading...

Share This Page