2

I tried to sort an array of string using qsort. here is the content of my array:

{"a","orange","apple","mobile","car"}

this is how I use qsort:

int myCompare (const void * a, const void * b ) {
  const char *pa = (const char*)a;
  const char *pb = (const char*)b;
  return strcmp(pa,pb);
}

int stringLen = sizeof(input)/sizeof(char *);
qsort(input, stringLen, sizeof (char*), myCompare);

However, when I print the array, nothing is changed. is there something wrong w/ this?

6
  • What is input declared as? Commented Feb 21, 2013 at 1:57
  • char *input = (char **)malloc((size+1)*sizeof(char)); Commented Feb 21, 2013 at 1:59
  • there's your problem! Commented Feb 21, 2013 at 2:00
  • an array of pointers to char, is not the same as a contiguous memory block of char of the same size. Commented Feb 21, 2013 at 2:05
  • 2
    Last try: please post all the relevant ACTUAL code. Commented Feb 21, 2013 at 2:37

3 Answers 3

10

I've changed your myCompare function to what Mitch Wheat had posted earlier, and that works properly. Here's the example:

#include <string.h>
#include <stdio.h>
#include <stdlib.h>

int myCompare (const void * a, const void * b ) {
    const char *pa = *(const char**)a;
    const char *pb = *(const char**)b;

    return strcmp(pa,pb);
}

int main() {
    int i;
    const char *input[] = {"a","orange","apple","mobile","car"};

    int stringLen = sizeof(input) / sizeof(char *);
    qsort(input, stringLen, sizeof(char *), myCompare);

    for (i=0; i<stringLen; ++i)
        printf("%d: %s\n", i, input[i]);
}

This will return:

0: a
1: apple
2: car
3: mobile
4: orange
Sign up to request clarification or add additional context in comments.

5 Comments

I just copy paste ur code, but it doesn't work T.T is it possible that something is wrong w/ the compiler?
No. As I said some time ago: it's how you declare your 'array'. THE COMPILER ISN"T BROKEN! Point of diminishing return reached....
@MitchWheat but according to sharth it works properly
It also works correctly here: ideone.com/UqvehY
To keep const qualifier between castings, these lines must change a little: const char * pa = *(const char * const * ) a; const char * pb = *(const char * const * ) b;
4

qsort(input, stringLen, sizeof (char*), myCompare) calls myCompare to compare strings which are sorted.

myCompare gets pointers to compared values. In our case we get pointers to strings (const char**). So we should compare *(const char**)a and *(const char**)b, which are strings pointed by a and b.

Comments

2

Start debugging with:

int myCompare (const void * a, const void * b ) {
  const char *pa = (const char*)a;
  const char *pb = (const char*)b;

  printf("Comparing %s vs. %s for result %d\n", pa, pb, strcmp(pa,pb));

  return strcmp(pa,pb);
}

I think shortly after that, you'll figure out the problem. :)

1 Comment

nothing is wrong. it return a correct value for every a and b

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.