How to compare structure elements

This is a discussion on How to compare structure elements within the C Programming forums, part of the General Programming Boards category; Hi, I've working on this for hours now... Here is my structure Code: #define MAX_ITEM 10 typedef struct { int ...

  1. #1
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165

    How to compare structure elements

    Hi,

    I've working on this for hours now...

    Here is my structure

    Code:
    #define MAX_ITEM 10
    
    typedef struct {
    
         int stk_num,items_in_stk,min_stk,itm_on_ord,supplier;
         float reord_price;
         char desc[26],ord_ref[9];
    } record;
    
    record List[MAX_ITEM];
    I am trying to display the contents in ascending order using qsort() function. This is how I am calling the sort function

    Code:
    qsort(List, MAX_ITEM, sizeof(List[0]), cmp_element);
    And this is my comp_element function with which I am having problem and I can't figure it out myself

    Code:
    int cmp_element(record *ptr1, record *ptr2 )
     {
         return strcmp(ptr1->desc , ptr2->desc);
    
     }
    But I am getting this error

    Code:
    [C++ Error] Stock4.c(206): E2342 Type mismatch in parameter '__fcmp' (wanted 'int (*)(const void *,const void *)', got 'int (*)(record *,record *)')
    I believe its a very minor problem somewhere but I can't find it myself and time is running out...

    Help please.....

  2. #2
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > int cmp_element(record *ptr1, record *ptr2 )
    Should be const void * parameters,
    cast to record* inside the function
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  3. #3
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    Quote Originally Posted by Salem
    > int cmp_element(record *ptr1, record *ptr2 )
    Should be const void * parameters,
    cast to record* inside the function
    Well I tried this before

    Code:
    int intcmp(const void *ptr1, const void *ptr2 )
     {
         return strcmp((record *)ptr1->desc, (record *)ptr2->desc);
    
     }
    But then I got this error...

    Code:
    [C++ Error] Stock4.c(223): E2288 Pointer to structure required on left side of -> or ->*
    I have no clue how to solve this

  4. #4
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    Precedence!
    ((record *)ptr1)->member

    Or just use another variable of the correct type
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

  5. #5
    Registered User khpuce's Avatar
    Join Date
    May 2003
    Posts
    165
    OMG !!!!!!!!

    and I spent hours on this....

    Anyway, thank you very much dear Salem. You saved me again


    Edit :
    vBulletin Message
    You must spread some Reputation around before giving it to Salem again.

  6. #6
    Registered User
    Join Date
    Apr 2005
    Posts
    12
    question:

    why must it be const void * parameters
    and then casted to record* inside the function. what was wrong with what he initially did?

  7. #7
    and the hat of wrongness Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    32,548
    > what was wrong with what he initially did?
    Because the pointer to the function was of the wrong type.
    qsort expects a pointer expecting two void* parameters, and returning int.
    If it isn't that, it complains.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.
    I support http://www.ukip.org/ as the first necessary step to a free Europe.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. passing structure arrays to functions?
    By bem82 in forum C Programming
    Replies: 3
    Last Post: 10-30-2006, 05:17 AM
  2. structure ...solution plz????
    By hegdeshashi in forum C Programming
    Replies: 4
    Last Post: 07-24-2006, 09:57 AM
  3. Binary Search Trees Part III
    By Prelude in forum A Brief History of Cprogramming.com
    Replies: 16
    Last Post: 10-02-2004, 03:00 PM
  4. structure compare
    By beely in forum C Programming
    Replies: 12
    Last Post: 10-25-2002, 07:47 PM
  5. referencing C structure elements in assembly
    By BigAl in forum C Programming
    Replies: 7
    Last Post: 09-10-2001, 03:19 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21