fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int compare(const void* a1, const void* a2)
  6. {
  7. const char** s1 = a1;
  8. const char** s2 = a2;
  9. const int result = strcmp(*s1, *s2);
  10. return result;
  11. }
  12.  
  13. int main()
  14. {
  15. char* s[] = { "e", "d", "c", "b", "a" };
  16. const size_t s_element_size = sizeof(s[0]);
  17. const size_t s_element_count = sizeof(s) / s_element_size;
  18. size_t i;
  19.  
  20. for (i = 0; i < s_element_count; i++)
  21. {
  22. printf("[%s]\n", s[i]);
  23. }
  24. printf("\n");
  25.  
  26. qsort(s, s_element_count, s_element_size, compare);
  27.  
  28. for (i = 0; i < s_element_count; i++)
  29. {
  30. printf("[%s]\n", s[i]);
  31. }
  32.  
  33. return 0;
  34. }
  35.  
Success #stdin #stdout 0.01s 1676KB
stdin
Standard input is empty
stdout
[e]
[d]
[c]
[b]
[a]

[a]
[b]
[c]
[d]
[e]