3

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));
}
7
  • 2
    qsort is your friend. Post your qsort code perhaps? Commented Oct 24, 2012 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 Commented Oct 24, 2012 at 13:50
  • If you know the size of the strings (10), why use strcpy instead of strncpy? Commented Oct 24, 2012 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 :-) Commented Oct 24, 2012 at 14:05
  • 1
    Thanks for the compliment..I'm glad it helped :) Commented Oct 24, 2012 at 14:08

1 Answer 1

7

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);
Sign up to request clarification or add additional context in comments.

3 Comments

Your cmp function is equivalent to strcmp. Just use qsort(stock, r, 10, strcmp);?
@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.
@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

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.