Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I need to sort an array of Strings in C. Here's how I'm using the 2D Array:

I, first, declare the array of size 115 with each element of the array having a capacity of 10 characters: char stock[115][10];

Then, as soon as the user enters a word that I need to put into this array, I store it like:

strcpy(stock[r],msg);
r++;

Where msg is the temporary variable used to store the user input and r is an integer initially assigned to 0.

Now the issue is when I need to print the Stock array. I need the output to be in a alphabetical order. I tried using qsort but could not get it to work, probably I didn't quite implement it properly due to lack of understanding of qsort.

Please suggest a method to sort the STOCK array so that I can print the expected output.

Also note that the usual printing of Stock Array is working fine, ie, if i try to print the array in order in which it was stored, it works fine. It's the sorting with which I need help.

Thanks :)

Edit01: The QSORT Method that I'm trying to use here is:

//Call Qsort Method
qsort(stock, r, sizeof(stock[0]), comp);



//Function to Compare two Strings - Used in the QSORT Method
int comp(const void *s1, const void *s2)
{
    return (strcmp(*(char **)s1, *(char **)s2));
}
share|improve this question
2  
qsort is your friend. Post your qsort code perhaps? –  William Morris Oct 24 '12 at 13:50
    
you can try to store your strings in the alphabetical order in the moment when you enter new string in the stock array –  MOHAMED Oct 24 '12 at 13:50
    
If you know the size of the strings (10), why use strcpy instead of strncpy? –  mlibby Oct 24 '12 at 13:53
1  
+1 for your approach of using sizeof(stock[0]) instead of hard coding the value. I liked this approach. I am learning C and your example set the standards for me to look at. BRILLIANT :-) –  nIcE cOw Oct 24 '12 at 14:05
1  
Thanks for the compliment..I'm glad it helped :) –  proctr Oct 24 '12 at 14:08
show 2 more comments

1 Answer

up vote 6 down vote accepted

Here is the prototype of qsort:

void qsort(void *base, size_t nmemb, size_t size, 
           int(*compar)(const void *, const void *));
  • base is your array (stock here).
  • nmemb is the number of members of your array (r here).
  • size is the size of an element (10 here).

The compar function should compare two strings, and return an integer less than, equal to, or greater than zero if the first argument is considered to be respectively less than, equal to, or greater than the second. strcmp can do it for you.

#include <string.h>

int cmp(const void *a, const void *b) { 
    return strcmp(a, b); 
}

qsort(stock, r, 10, cmp);
share|improve this answer
    
Your cmp function is equivalent to strcmp. Just use qsort(stock, r, 10, strcmp);? –  Frerich Raabe Oct 24 '12 at 14:00
    
Thanks. It worked! :) –  proctr Oct 24 '12 at 14:02
    
@FrerichRaabe: strcmp and compare prototypes are not the same. In the best-case scenario, you need a function pointer cast. In the worst-case scenario, it is an undefined behavior, because const char * and const void * are not compatible types. –  md5 Oct 24 '12 at 14:08
    
@Kirilenko what you said is true for c++. But i think it would be fine with c since it does not require explicit casts and we are always passing c strings in this scenario –  fayyazkl Oct 24 '12 at 17:37
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.