Thread: Using qsort on string array.

  1. #1
    Registered User
    Join Date
    Apr 2012
    Posts
    7

    Using qsort on string array.

    Hi, I'm a bit of a novice C programmer and I'd like help with what I'm trying to do. I've got a rather large array of strings implemented as a char** and I'm trying to sort it for later processing. If anyone could tell me where and what I'm doing wrong, it'd be greatly appreciated. At the moment it's not altering the ordering of the array at all. I tried to post more details but it seems the forum's cutting me off.

  2. #2
    Registered User
    Join Date
    Sep 2006
    Posts
    8,868
    We have first timer's post code all the time, so I'm not sure what's up.

    Post what you're working with. That's a requirement of the board, actually. qsort() has some tricky casts that need to be made.

  3. #3
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Not sure what was happening. It was just cutting my post off halfway through my first snippet when I tried. Anyway, here's the qsort() call and the comparator I give it:

    Code:
    void sort_dict(){
         qsort(words, dicSize, sizeof(char)*30, cstring_cmp);
    }
    
    int cstring_cmp(const void *a, const void *b) { 
        const char **ia = (const char **)a;
        const char **ib = (const char **)b;
        return strcmp(*ia, *ib);
    }
    And the array declaration and allocation:

    Code:
    char **words;
    
       words = (char**)malloc(sizeof(char*)*dicSize);
       for(i = 0; i<dicSize; i++){
          words[i] = (char*)malloc(sizeof(char)*30);
       }

  4. #4
    Master Apprentice phantomotap's Avatar
    Join Date
    Jan 2008
    Posts
    5,108
    Code:
    sizeof(char)*30
    O_o

    What did you get this? That parameter marks the size of each element of the things to be sorted. You are not sorting an array of things that is 30 `char' in size. You are sorting an array of pointers; things that are `sizeof(char *)' in size. You even said so in a different bit of code. The `qsort' functions doesn't care what the things it sorts are underneath. Only the comparison function cares about what a thing really is underneath.

    Soma

  5. #5
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    Well, that was exceptionally stupid of me - I'm still not that great with pointers. I changed the call to:
    Code:
     qsort(words, wordCount, sizeof(char*), cstring_cmp);
    And it's working now. It's always some stupid mistake, it seems. Thanks for the help.

  6. #6
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    The thing you want to sort is an array of char*'s hence sizeof(char*) is appropriate for the third parameter.
    The length of what each pointer is doesn't come into it as strcmp just goes up until the null-terminator.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  7. #7
    Registered User
    Join Date
    Apr 2012
    Posts
    7
    I've run into further problems with the same code, when I've tried to move to significantly larger examples.

    On test input which dealt with arrays of length less than 100, this worked fine, however for the purpose I need it needs to deal with a string array of length 250000. (I'm trying to sort a dictionary that my parser has generated).

    Unfortunately, when I try this, the program crashes on the qsort statement. Am I simply giving it too big an input, or is there something else going on?

  8. #8
    Registered User
    Join Date
    Aug 2005
    Location
    Austria
    Posts
    1,990
    We would have to see how you're defined that stringarray. Also what compiler you use, your operating system and also the range of an int on you system could be smaller then 250000.
    Kurt

  9. #9
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    Possibly some of your malloc calls failed. I don't know what you're doing with regards to putting data into each string, but the compare function as you have it now, would definitely crash on the NULL pointers.
    Tell us more about the crash. Usually they say something about reading or writing, or invalid instruction etc, and possibly some hexadecimal number. Please copy and paste the message you get here.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. qsort and array
    By ollie in forum C Programming
    Replies: 24
    Last Post: 08-16-2010, 01:34 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. qsort( ) on a structured array
    By Thumper333 in forum C Programming
    Replies: 2
    Last Post: 10-21-2004, 07:39 PM
  4. Qsort and Dynamic Array
    By Thantos in forum C Programming
    Replies: 5
    Last Post: 12-12-2003, 06:36 PM
  5. Qsort to an Array?
    By Unregistered in forum C Programming
    Replies: 5
    Last Post: 11-27-2001, 02:19 PM