Sort structure problem

This is a discussion on Sort structure problem within the C Programming forums, part of the General Programming Boards category; Hi there, I'm using a large structure, and I want to sort it by one of its members, a double ...

  1. #1
    Registered User
    Join Date
    Jun 2009
    Posts
    12

    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:

    Code:
    //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,
    Code:
    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:
    Code:
    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
    Location
    Toronto, Canada
    Posts
    1,832
    Get rid of the pointer thingy.

    Code:
    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, 07: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, 06:46 PM
  4. Insertion sort problem...
    By BigFish21 in forum C++ Programming
    Replies: 4
    Last Post: 05-27-2008, 01:53 AM
  5. Straight Insertion Sort function problem
    By StaticKyle in forum C++ Programming
    Replies: 6
    Last Post: 05-12-2008, 05:03 AM

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