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 8952115
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T13:48:15+00:00 2026-06-15T13:48:15+00:00

Okay so I need to several quite long strings in C. So I say

  • 0

Okay so I need to several quite long strings in C. So I say to myself “why, you’d better use that handy dandy qsort function! Better write yourself a string_comparator for it!”

So of course I do and here she is:

int string_comparator(const void* el1, const void* el2) {

char* x = (char*) el1;
char* y = (char*) el2;

int str_len = strlen(x);
int i = 0;
for (; i < str_len; i++) {

    //when there are non-equal chars
    if (x[i] != y[i]) {
        break;
    }
}

return x[i] - y[i];
}

So of course I pass my handy dandy string_comparator function to the C qsort function as such:

qsort(list.words, list.num_words, sizeof(char*), string_comparator);

list is a struct that holds a char** (words) and ints which refer to the number of words held by it (such as num_words)

Now I have the problem where my list is not getting sorted alphabetically like I had hoped! I put a bunch of printf statements in my comparator and it printed out garbage values for the strings every time so I’m fairly sure that is the problem. But why is that the problem?? I’ve used qsort before (never to sort words..just sorting characters) and from what I understand this should work…What’s going wrong here?

I appreciate any suggestions!

  • 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-15T13:48:17+00:00Added an answer on June 15, 2026 at 1:48 pm

    This is a common mistake when using qsort(). Here are the corrections:

    char *x = *(char **) el1;
    char *y = *(char **) el2;
    

    Because list.words has type char **, not type char *, right?

    Another example of qsort()

    Here’s how you sort an array of int with qsort():

    int int_comparator(const void *el1, const void *el2)
    {
        int x = *(int *) el1;
        int y = *(int *) el2;
        return x - y;
    }
    
    void sort_ints(int *a, size_t n)
    {
        // these two lines are both "correct"
        // the second line is more "obviously correct"
    
        // qsort(a, n, sizeof(int), int_comparator);
        qsort(a, n, sizeof(*a), int_comparator);
    }
    

    Now, if you go through and replace int with char *, you have to replace int * with char **.

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

Sidebar

Related Questions

Okay, I need some help here. This is the same old "can't use an
Okay so I need to make a search that is set up something like:
Okay! I really need your help or some tips I need a program that
Okay basically I have an MFC application with lots of dialogs that need to
Okay, my issue is better explained visually: I need to make those 5 images
Okay, so I have a few possible matches that I need to test. It
ok there are several similar questions but not quite anything that I want. I
Okay so I need to push the array that is fetched from a few
Okay, I think I need another pair of eyes to look over this. I'm
Okay, I really need some help with this. I'm new with designing and stuff,

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.