Search code examples
cstringpointersalphabeticaldouble-pointer

How to make an alphabetical sort on double pointers in c?


qsort(Names, numberOfFriends, 50, strcmp);

I tried to make an alphabetical sort on Names (wich is a double pointer). every string in the double pointer is dynamically allocated string, wich means every string have a different size. One of the parameters of the qsort function, is the size of the strings (it's actually a function that ment to 2d array, and not for double pointers with dynamically allocated strings).

How can i do an alphabetical sort on double pointers that have dynamically allocated strings?


Solution

  • Partial answer

    The number of characters in the string is not so important - see this question - strcmp will compare the first n bytes of the arrays where the first n-1 bytes are the same.

    so to compare Names[i] and Names[j] you should be able to use

    differentce=strcmp(Names[i], Names[j]);
    

    provided that Names is declared as char ** Names.

    If you want more help with how to do this I suggest you put more code in your question.