1

the question is simple: there is some way that the ordered array that returns me the "qsort", is returned in reverse, ie I want to avoid the use of any auxiliary array to invest the resulting array using qsort.

this is my code, which reads from standard input strings to be sorted, and uses a comparison function for sorting.

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

            int cstring_cmp(const void *a, const void *b)
            {
                const char **ia = (const char **)a;
                const char **ib = (const char **)b;
                return strcasecmp(*ia, *ib);
                /* strcmp functions works exactly as expected from
                comparison function */
            }

Thanks in advance for your response, sorry for my English

            int main (int argc, char *argv [])

            {
            int number;
            char temp [4000];

            printf("input number: ");
            scanf("%d",&number);

            char* array_string [number];
            int i;
            for (i=0;i<number;i++) {
            scanf(" %[^\n]", temp);
            array_string [i] = (char*)malloc((strlen(temp)+1)*sizeof(char));
            strcpy(array_string[i], temp);
            }


            size_t large = sizeof(array_string) / sizeof(char *);
            qsort(array_string,large ,sizeof(char *) ,cstring_cmp );
            printf ("\n");
            printf ("the sorted array list is:\n");
            for (i=0;i<large;i++)
            printf("%s\n", array_string [i]);
                    return 0;
            }
1
  • return -1 * strcasecmp(*ia, *ib); might work. If you reverse the comparison function the array will be reversed. Commented Oct 31, 2010 at 0:02

2 Answers 2

4

Have you just tried reversing the order of parameters to strcasecmp?

return strcasecmp(*ib, *ia);

Sign up to request clarification or add additional context in comments.

Comments

3

Does this do what you want?

        int cstring_cmp(const void *a, const void *b)
        {
            const char **ia = (const char **)a;
            const char **ib = (const char **)b;
            return -strcasecmp(*ia, *ib);
            /* return the negative of the normal comparison */
        }

Comments

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.