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

  • Home
  • SEARCH
  • 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 8447527
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 10, 20262026-06-10T10:11:57+00:00 2026-06-10T10:11:57+00:00

I want to sort a list of string. To parameter of qsort() function is

  • 0

I want to sort a list of string. To parameter of qsort() function is a list of c[i]’s defined as so :

    //N is the number of words
    int N;
    scanf("%d",&N);
    //each c[i] is a word 
    char **c;
    c = malloc(N*sizeof(char*));

    for(i = 0; i < N; i++)
    {
      char *temp[100];
      scanf("%s", &temp);
      c[i]=strdup(temp);
    }

But when I sort the list, the result is wrong.

Precisely to debug, I added this

for(i = 0; i < N; i++){
  j=compare (c[0],c[i]);
  printf("%d",j);
  printf("%s",c[i]);
}

My input is ‘4;a;b;c;d’ and I get ‘ 0b-1c-1d1a’

Can anybody help about this?

  • 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-10T10:11:59+00:00Added an answer on June 10, 2026 at 10:11 am

    This is incorrect:

    char *temp[100];
    scanf("%s", &temp);
    

    temp is an array of 100 uninitialised char* pointers, there is no storage for a char[]. Change to:

    char temp[100];
    scanf("%99s", temp); /* Added maximum number of chars to prevent overrun. */
    

    From you comment:

    int compare(const void* l, const void* r)
    {
        return strcmp(l, r);
    }
    

    This should be:

    int compare(const void* a1, const void* a2)
    {
        const char** s1   = a1;
        const char** s2   = a2;
        return strcmp(*s1, *s2);
    }
    

    From the C99 standard section 7.20.5.2 The qsort function:

    The contents of the array are sorted into ascending order according to a comparison
    function pointed to by compar, which is called with two arguments that point to the
    objects being compared.
    The function shall 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.

    Which means that the type of the arguments to the compare() function are char**, and therefore need to be dereferenced before performing a string comparision.

    See http://ideone.com/rUG89 for example.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

List<Tuple<String, int[], int>> I want to sort the List by the int from highest
I want to sort a list of strings based on the string length. I
I have List I want to sort Desc by Priority, which is int and
So I have a List of Dictionary<string, string> dict and I want to sort
I have a function that takes IList<string> someVariable as a parameter. I want to
How do I sort List<List<string>> according to number of fields? The list got structure
I want to sort list items by their priority, which the user types in,
I want to sort a list of integer values, but before sorting them I
I have a list of artists, albums and tracks that I want to sort
I have an array containing a list of files. I want to sort it

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.