Thread: Sort structure problem

  1. #1
    Registered User
    Join Date
    Jun 2009

    Sort structure problem

    Hi there,

    I'm using a large structure, and I want to sort it by one of its members, a double variable.
    This is the code I would like to implement:

    //RMSD Sort, this one works OK.
    qsort(outlib, structs_len, sizeof(res), struct_cmp_by_rmsd);
    //then the structure needs to be re-sorted
    qsort(outlib, structs_len, sizeof(res), struct_cmp_by_Model);
    These are the definitions I'm using,
    int struct_cmp_by_rmsd(const void *a, const void *b)
        res *ia = (res *)a;
        res *ib = (res *)b;
        return (int)(10000.f*ia->rmsd_ext - 10000.f*ib->rmsd_ext);
        /* float comparison: returns negative if b > a  and positive if a > b. We multiplied result by 10000.0 to preserve decimal fraction */
    int struct_cmp_by_Model(const void *a, const void *b)
        res *ia = (res *)a;
        res *ib = (res *)b;
        return (int *) ( ia->numModel - ib->numModel );
    after compiling, I get this error:
    sort_structures.h: In function `int struct_cmp_by_Model(const void*, const void*)':
    sort_structures.h:19: warning: cast to pointer from integer of different size
    sort_structures.h:19: error: invalid conversion from `int*' to `int'
    I could not get this one to work, any help would be really appreciated.
    Thanks in advance,

    Juan Pablo

  2. #2
    Registered User
    Join Date
    Sep 2008
    Toronto, Canada
    Get rid of the pointer thingy.

    return (int) ( ia->numModel - ib->numModel );

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. dynamic array and structure problem
    By bluetxxth in forum C Programming
    Replies: 3
    Last Post: 04-13-2010, 06:56 AM
  2. Urgent problem with a merge sort.
    By jonnoblood in forum C Programming
    Replies: 7
    Last Post: 02-10-2010, 08:35 PM
  3. Problem referencing structure elements by pointer
    By trillianjedi in forum C Programming
    Replies: 19
    Last Post: 06-13-2008, 05:46 PM
  4. Insertion sort problem...
    By BigFish21 in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2008, 12:53 AM
  5. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 04:03 AM