Urgent Help - Qsort

This is a discussion on Urgent Help - Qsort within the C Programming forums, part of the General Programming Boards category; Hi I am trying to qsort a struct, here is my code for my struct: Code: struct pers { char ...

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Urgent Help - Qsort

    Hi I am trying to qsort a struct, here is my code for my struct:

    Code:
    struct pers
    {
    	char *name;
    	int age;
    };
    
    typedef struct queue
    {
    	int number;
    	struct pers person[400];
    }cc;
    
    cc p[21];
    I have trying to work this out for days, but cant get my head around it.
    At this point the "struct pers person[400];" contains data which is linked to the struct pers, i need to use a qsort to sort [asc] based on the persons age.

    Please any suggestions? You help would be greatly appreciated.

    Thanks

  2. #2
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    I have trying to work this out for days, but cant get my head around it.
    Show us something you've tried and someone will tell you why it doesn't work. Or ask a more specific question about the compare function you use with qsort (that is, the 4th argument ). I'm not going to write the code for you.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    399
    You can begin with showing us the comparison function you're passing to qsort.

  4. #4
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Qsort function

    This is my qsort function:
    Code:
    for(j=0; j < 22; j++)
    {
        qsort(p[j].person,p[j].number,sizeof(struct queue),struct_cmp);
    }
    And my (struct_cmp) comparison function consists of:

    Code:
    int struct_cmp(const void *a, const void *b) /* Compare integer function */
    {	
        cc *p1 = (cc*)a;
        cc *p2 = (cc*)b;
    
    	if(p1->person->age > p2->person->age)
    		return 1;
    	else
    		return 0;
    }
    Im guessing this is total rubbish, i just get stuck with pointers and structs.
    Last edited by it01y2; 01-16-2010 at 11:03 AM.

  5. #5
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Okay, qsort arguments are (your second one is definitely wrong):

    1) a pointer to your struct array
    2) the number of elements in the array (how bout 400?)
    3) the sizeof one element
    4) the compare function pointer

    If you are using a struct array, you cannot use a compare function which accepts two ints. It must accept two structs and compare the relevent fields within them.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  6. #6
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Qsort function

    Quote Originally Posted by MK27 View Post
    Okay, qsort arguments are (your second one is definitely wrong):

    1) a pointer to your struct array
    2) the number of elements in the array (how bout 400?)
    3) the sizeof one element
    4) the compare function pointer

    If you are using a struct array, you cannot use a compare function which accepts two ints. It must accept two structs and compare the relevent fields within them.
    Thanks for the tips dude, really appreciate it. Its just implementing this now, I will give it a go.

  7. #7
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Point 3

    Code:
    qsort(q[j].person,400,sizeof(struct queue),struct_cmp);
    I am not sure how to get the sizeof one element?

  8. #8
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by it01y2 View Post
    Code:
    qsort(q[j].person,400,sizeof(struct queue),struct_cmp);
    I am not sure how to get the sizeof one element?
    sizeof(struct pers)? I am not awake yet...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  9. #9
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    Code:
    qsort(q[j].person,400,sizeof(struct queue),struct_cmp);
    I am not sure how to get the sizeof one element?
    Since q[j].person is an array of struct pers, then each element is sizeof(struct pers), not sizeof(struct queue). qsort needs to know how big each unit of the array is, since the pointer (1st arg) is a void type, so the "kind" of array cannot be determined by qsort internally. It has to send 2X "3rd arg" bytes of data at a time to the compare function.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

  10. #10
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Qsort function

    KK i see, ty

  11. #11
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Qsort function

    In the function im going to have to pass the struct by pointer into the compare function, then access the childs to do a comparison im guessing so?

  12. #12
    Registered User jeffcobb's Avatar
    Join Date
    Dec 2009
    Location
    Henderson, NV
    Posts
    875
    Quote Originally Posted by it01y2 View Post
    In the function im going to have to pass the struct by pointer into the compare function, then access the childs to do a comparison im guessing so?
    Add a cast to the correct type and I think that you are there...

    God I hope my caffeine kicks in soon...
    C/C++ Environment: GNU CC/Emacs
    Make system: CMake
    Debuggers: Valgrind/GDB

  13. #13
    Registered User
    Join Date
    Dec 2009
    Posts
    21
    *shrug* I really dont understand this, how would you do it?

  14. #14
    Registered User
    Join Date
    Dec 2009
    Posts
    21

    Sorting doesn't work

    I have tried to create the compare function but im doing something wrong:

    Code:
    int struct_cmp(const void *a, const void *b) /* Compare integer function */
    {	
    	struct pers *ia = (struct pers *)a;
    	struct pers *ib = (struct pers *)b;
    	if(ia->age > ib->age)
    		return -1;
    	else
    		return 1;
    }
    Is this correct?

    Thanks

  15. #15
    spurious conceit MK27's Avatar
    Join Date
    Jul 2008
    Location
    segmentation fault
    Posts
    8,300
    Quote Originally Posted by it01y2 View Post
    I have tried to create the compare function but im doing something wrong:
    I would stick some printf's in there, or use a debugger, to check the values and see what's happening.
    C programming resources:
    GNU C Function and Macro Index -- glibc reference manual
    The C Book -- nice online learner guide
    Current ISO draft standard
    CCAN -- new CPAN like open source library repository
    3 (different) GNU debugger tutorials: #1 -- #2 -- #3
    cpwiki -- our wiki on sourceforge

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. An interesting problem of qsort()
    By meili100 in forum C++ Programming
    Replies: 6
    Last Post: 03-05-2008, 12:09 PM
  2. trouble with qsort
    By qubit67 in forum C Programming
    Replies: 5
    Last Post: 04-29-2007, 11:23 PM
  3. How to write my own qsort routine
    By tonbarius in forum C Programming
    Replies: 3
    Last Post: 05-14-2006, 01:34 PM
  4. malloced 2d array passed to qsort
    By jamie85 in forum C Programming
    Replies: 7
    Last Post: 11-25-2005, 01:55 PM
  5. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 03:22 AM

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