qsort - HP UX

This is a discussion on qsort - HP UX ; Hi, i am getting a warning when compiling my program... blbp_eom_isr_revenue.c", line 739: warning 527: Integral value implicitly converted to pointer in assignment. blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the correct type. and at line 739:- qsort((void ...

+ Reply to Thread
Results 1 to 4 of 4

Thread: qsort

  1. qsort

    Hi,
    i am getting a warning when compiling my program...
    blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
    implicitly converted to pointer in assignment.
    blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
    correct type.

    and at line 739:-

    qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
    (*blbp_CtnRecCompare));

    and the function blbp_CtnRecCompare is -
    int blbp_CtnRecCompare (strcture_name Rec_1,
    strcture_name Rec_2)
    {
    return (strcmp (Rec_1.field_name, Rec_2.field_name));
    }

    Thanks for the solutions.


  2. Re: qsort

    On 2006-09-08, kc wrote:

    I think this belongs in a C group, not an HP-UX group.

    > blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
    > implicitly converted to pointer in assignment.
    > blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
    > correct type.



    > qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
    > (*blbp_CtnRecCompare));


    C doesn't much care about the line breaks, so split it up
    and get a better idea where the 2 warnings come from when
    each line number points to only one argument.

    qsort((void *)Buff,
    totalRecs,
    sizeof(strcture_name),
    (int) (*blbp_CtnRecCompare));


    > and the function blbp_CtnRecCompare is -
    > int blbp_CtnRecCompare (strcture_name Rec_1,
    > strcture_name Rec_2)
    > {
    > return (strcmp (Rec_1.field_name, Rec_2.field_name));
    > }



    I think the comparison function wants to take 2 pointers rather than
    2 structures.


    --
    Elvis Notargiacomo master AT barefaced DOT cheek
    http://www.notatla.org.uk/goen/
    One of my other 11 computers runs Minix.

  3. Re: qsort

    kc wrote:
    > Hi,
    > i am getting a warning when compiling my program...
    > blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
    > implicitly converted to pointer in assignment.
    > blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
    > correct type.
    >
    > and at line 739:-
    >
    > qsort((void *)Buff, totalRecs, sizeof(strcture_name), (int)
    > (*blbp_CtnRecCompare));
    >
    > and the function blbp_CtnRecCompare is -
    > int blbp_CtnRecCompare (strcture_name Rec_1,
    > strcture_name Rec_2)
    > {
    > return (strcmp (Rec_1.field_name, Rec_2.field_name));
    > }
    >
    > Thanks for the solutions.


    Well, there are two problems with this code.
    First, as someone else already pointed out, comparison function expects
    two pointers, not arbitrary structs.
    So, your comparison function should look like:
    int blbp_CtnRecCompare (void * Rec_1,
    void * Rec_2)
    {
    structure_name *rec1 = (structure_name *)Rec_1;
    structure_name *rec2 = (structure_name *)Rec_2;

    /* XXX: do some handling of NULL cases */

    return (strcmp (rec1->field_name, rec2->field_name));
    }

    Second, trust the compiler - if it's saying arg#4 is not the correct
    type, then 99 out of 100 cases arg#4 is not the correct type.
    The type of something like this (int) (*blbp_CtnRecCompare) is int,
    since you did an explicit cast. I won't get into discussion of the
    meaning of (*blbp_CtnRecCompare).
    You tried to pass a function pointer, but that's not what your syntax
    accomplishes. However, once your comparison function is prototyped as
    int blbp_CtnRecCompare(void *, void*);
    there is no need to cast the function address in the qsort call. Just
    make the call as:
    qsort((void *)Buff, totalRecs, sizeof(strcture_name),
    blbp_CtnRecCompare);
    and you should be fine.
    Good luck,
    Jozek


  4. Re: qsort

    >
    > > blbp_eom_isr_revenue.c", line 739: warning 527: Integral value
    > > implicitly converted to pointer in assignment.
    > > blbp_eom_isr_revenue.c", line 739: warning 563: Argument #4 is not the
    > > correct type.


    > qsort((void *)Buff,
    > totalRecs,
    > sizeof(strcture_name),
    > (int) (*blbp_CtnRecCompare));


    the prototype for qsort(3C) is

    void qsort(
    void *base,
    size_t nel,
    size_t size,
    int (*compar)(const void *, const void *)
    );

    the last parameter is not int, its a pointer to a function that returns
    int.
    so blbp_CtnRecCompare should not be cast to int. this is what the
    warning is about.

    > > and the function blbp_CtnRecCompare is -
    > > int blbp_CtnRecCompare (strcture_name Rec_1,
    > > strcture_name Rec_2)
    > > {
    > > return (strcmp (Rec_1.field_name, Rec_2.field_name));
    > > }

    >
    >
    > I think the comparison function wants to take 2 pointers rather than
    > 2 structures.


    qsort expects a pointer to a function that takes pointer parameters,
    not
    struct's.


+ Reply to Thread