qsort

This is a discussion on qsort within the C Programming forums, part of the General Programming Boards category; I am trying to sort an array of strings (an array of character arrays). It says in qsort's man page ...

  1. #1
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501

    qsort

    I am trying to sort an array of strings (an array of character arrays). It says in qsort's man page that it can sort single dimensional arrays. Is there a way I can use qsort to sort my array? and please give the the function to be used as the forth arg, i can't seem to get it right.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    I'll assume you mean you're using a two dimensional character array? Yes? Ok, pass the array name as the object to be sorted, and write your sort function so that it copies the contents of one row to a temp variable, copy the other row over top of the first, and then the temp array over the second:

    char temp[BUFSIZ];

    strcpy( temp, array[x] );
    strcpy( array[x], array[y] );
    strcpy( array[y] temp );

    Something similar to the above. However, the ideal way to do it, would be to just use an array of pointers to characters. That is to say:

    char (*array)[ELEMENTS];

    You then have an array of pointers, which you manipulate. Instead of actually moving the contents of the string, you just shuffle the pointers around.

    You could even use this "index" to be considered your sorted list, when in fact, you have your original array/2d array unchanged.

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I was hoping I could use the qsort() function because I want to learn how to use it.

  4. #4
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Originally posted by chrismiceli
    I was hoping I could use the qsort() function because I want to learn how to use it.
    Right, I was telling you how to use Qsort. Use the second of my two examples. Ignore my first method. I was getting myself mixed up on the comparison function, thinking you passed a sort instead.

    Something like:
    Code:
    char mystrings[STRINGS][LENGTH];
    char (*index)[STRINGS];
    int x;
    
    for( x = 0; X < STRINGS; x++ )
        index[x] = mystrings[x];
    You now have an index which you can pass to qsort. Write a function to compare the contents of what's being pointed at. Something like strcmp.

    Quzah.
    Last edited by quzah; 06-27-2003 at 10:06 PM.
    Hope is the first step on the road to disappointment.

  5. #5
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I am lost, here is what I have so far, I think I am way off base though
    Code:
    /* the qsort call */
    
    qsort(names, 12, (sizeof (char)) * 12, comp);
    
    /* the names array looks like this */
    char names[12][20];
    ...
    
    /* the comp func */
    int comp(char *str1, char *str2) {
    }
    the compiler won't compile that saying incompatible pointer types.

  6. #6
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Take a look at the above code. It's much simpler to sort an index of pointers than it is to sort a two dimensional array.

    Quzah.
    Hope is the first step on the road to disappointment.

  7. #7
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    i get incompatible types in assigment errors, here is my code

    Code:
    char (*index)[20];
    short x;
    for(x=0;x<12;x++) {
        index[x] = seats[x].name.last;
    }
    seats is an array of structures, there is 12 structures also.

  8. #8
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Yeah, give me a second. I'm about out the door. I did my assignment wrong. (I'll blame the phone.)

    [edit]
    Don't mind me. Remove the parenthesis. That give you a pointer to an array of characters. For some reason I got it into my head that I needed parenthesis there. I'm not sure why. Heh.
    [/edit]

    Quzah.
    Last edited by quzah; 06-27-2003 at 10:22 PM.
    Hope is the first step on the road to disappointment.

  9. #9
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    here is what I have so far, can someone help, I get this error using gcc

    plane.c:98: warning: passing arg 4 of `qsort' from incompatible pointer type

    here is the code
    Code:
    void showasa(void) {
            char *index[12];
            short x;
    
            for(x=0;x<12;x++) {
                    index[x] = seats[x].name.last;
            }
            qsort(index, 12, sizeof(char *), comp);
    }
    
    /* seats is an array of structures with a structure with a  string in it */
    
    /*start of a sorting func */
    int comp(char *str1[20], char *str2[20]) {
    }
    Can someone please help. thanx.
    Last edited by chrismiceli; 06-27-2003 at 11:15 PM.

  10. #10
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    thanx, now I am going to try and reword it with pointers, I really appreciate your help.

  11. #11
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    This is my attempt using pointers, The way you told me worked, i just want to get it to work this way. I think the prob is with my compar function, it compiles but I get output

    miceli
    ZY$�

    monroe
    �EE<fu�E�E}wʋU�


    here is my compar function
    Code:
    int comp(const void *a, const void *b) {
            char *pa, *pb;
            pa = (char *) a;
            pb = (char *) b;
            return strcmp(pa, pb);
    }
    here is my function that calls qsort

    Code:
    void showasa(void) {
            char *index[20];
            short x;
    
            for(x=0;x<12;x++) {
                    *(index+x) = seats[x].name.last;
            }
            qsort(index, sizeof(index) / sizeof(index[0]), sizeof(index[0]), comp);
            for(x=0;x<12;x++) {
                    printf("%s\n",index[x]);
            }
            putchar('\n');
    }
    I really appreciate your help so far.

  12. #12
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    I must be dense, everything i try gets seg fault. here is my current code, hopefully with the revisions in the right direction.

    Code:
    void showasa(void) {
            char *index[12];
            short x, count;
    
            for(x=0;x<12;x++) {
                    *(index+x) = seats[x].name.last;
            }
    */ --> seg fault here */      qsort(index, sizeof(index) / sizeof(index[0]), sizeof(index[0]), comp);
            for(x=0;x<count;x++) {
                    printf("%s\n",*(index + x));
            }
            putchar('\n');
    }
    
    int comp(const void *a, const void *b) {
            char **pa, **pb;
            *pa = (char *) a;
            *pb = (char *) b;
            return strcmp(*pa, *pb);
    }

  13. #13
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    seats[x].name.last is an array of structures with a structure inside of it with the array of chars that make up the last name of a person.
    Last edited by chrismiceli; 06-28-2003 at 04:20 PM.

  14. #14
    Obsessed with C chrismiceli's Avatar
    Join Date
    Jan 2003
    Posts
    501
    Thank you!!!! I owe you big man, I really appreciate your help and tolerance.

    <edit> if you couldn't guess I got it working.</edit>

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, 11:09 AM
  2. qsort() in Array of Pointer to String
    By vb.bajpai in forum C Programming
    Replies: 8
    Last Post: 06-16-2007, 04:18 PM
  3. trouble with qsort
    By qubit67 in forum C Programming
    Replies: 5
    Last Post: 04-29-2007, 10:23 PM
  4. Question About Using qsort
    By Zildjian in forum C Programming
    Replies: 3
    Last Post: 11-04-2003, 02:17 PM
  5. C++ link error with qsort
    By bvnorth in forum C++ Programming
    Replies: 7
    Last Post: 10-24-2003, 02:22 AM

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