Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8749161
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T12:38:54+00:00 2026-06-13T12:38:54+00:00

I need to sort an array of Strings in C. Here’s how I’m using

  • 0

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));
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T12:38:55+00:00Added an answer on June 13, 2026 at 12:38 pm

    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);
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I need make a merge sort using an additional array. Here is my code:
I have this array and I need to sort it, first by key_1 and
I have a two dimensional array that I need to sort numerically. Here is
I have an array of strings I need to sort in JavaScript, but in
OK, here is my problem. I need to take a array of Strings and
how to sort array of strings in php? Here strings are the paths. e.g.
I need to sort an array that can look like this: $array[4][0] = array('id'
I need to sort an array of months (alphabetical months) according to their months
I have an array which i need to sort its elements by occurrence then
hello guys i need to sort some elements of integer in an integer array

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.