Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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;
            }
share|improve this question
    
return -1 * strcasecmp(*ia, *ib); might work. If you reverse the comparison function the array will be reversed. –  Ronny Brendel Oct 31 '10 at 0:02

2 Answers 2

up vote 3 down vote accepted

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 */
        }
share|improve this answer

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

return strcasecmp(*ib, *ia);

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.