0

I am doing a school project with arrays using C. At the moment I am trying to sort the array of string to alphabetical order. I just can't seem to successfully do this. Here is the simplified code that I have done so far:

void sort_string_array(char **table)
{
    int i = 0;
    while (table[i++] != NULL);                // to get the length
    qsort(table, i, sizeof(char *), strcmp);   // sorting
}

Is this a completely wrong solution style, am I close, what is wrong :P ? Any help would be awesome!

EDIT:

void sort_string_array(char **table)
{
    int i = 0;
    while (table[i] != NULL) i++;              // to get the length
    qsort(table, i, sizeof(char *), strcmp);   // sorting
}

After correcting that error it is still not fuctioning right. Using string {'one','two','three','four'} first value of this sort should be 'four' but it is 'two'}

12
  • Possible duplicate of How to sort an array of string alphabetically (case sensitive, nonstandard collation) Commented Mar 30, 2016 at 10:38
  • Is this code not working? Commented Mar 30, 2016 at 10:41
  • 1
    @2501, the comparison function is strcmp :) Commented Mar 30, 2016 at 10:43
  • 1
    You cannot use strcmp directly as an comparison function: it has wrong argument types. You need to make a wrapper function for it. Commented Mar 30, 2016 at 10:44
  • 1
    Step through the code and watch the value of i in your debugger. Should take you less than 5 minutes to find the bug. Commented Mar 30, 2016 at 10:48

1 Answer 1

2

You are using one more item and probably accessing outside the bounds of array

while (table[i++] != NULL);  

should be

while (table[i] != NULL) i++;  

There is a reference in the C-FAQ about using qsort and strcmp:

Q: I'm trying to sort an array of strings with qsort, using strcmp as the comparison function, but it's not working.

A: By array of strings you probably mean array of pointers to char. The arguments to qsort's comparison function are pointers to the objects being sorted, in this case, pointers to pointers to char. strcmp, however, accepts simple pointers to char. Therefore, strcmp can't be used directly. Write an intermediate comparison function like this:

/* compare strings via pointers */
int pstrcmp(const void *p1, const void *p2)
{
    return strcmp(*(char * const *)p1, *(char * const *)p2);
}

The comparison function's arguments are expressed as generic pointers, const void *. They are converted back to what they really are (pointers to pointers to char) and dereferenced, yielding char *'s which can be passed to strcmp.

The call to qsort might look like

#include <stdlib.h>
char *strings[NSTRINGS];
int nstrings;
/* nstrings cells of strings[] are to be sorted */
qsort(strings, nstrings, sizeof(char *), pstrcmp);

(Don't be misled by the discussion in K&R2 Sec. 5.11 pp. 119-20, which is not discussing the Standard library's qsort, and makes a quiet, unnecessary assumption about the equivalence of char * and void *).

For more information on qsort comparison functions--how they are called and how they must be declared--see question 13.9.

References: ISO Sec. 7.10.5.2 H&S Sec. 20.5 p. 419

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

1 Comment

Oh my, what an stypid mistake I made, thanks a lot :)

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.