strinput0 2.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485
  1. QSORT(3) Linux Programmer's Manual QSORT(3)
  2. NAME
  3. qsort - sorts an array
  4. SYNOPSIS
  5. #include <stdlib.h>
  6. void qsort(void *base, size_t nmemb, size_t size,
  7. int(*compar)(const void *, const void *));
  8. DESCRIPTION
  9. The qsort() function sorts an array with nmemb elements of size size.
  10. The base argument points to the start of the array.
  11. The contents of the array are sorted in ascending order according to a
  12. comparison function pointed to by compar, which is called with two
  13. arguments that point to the objects being compared.
  14. The comparison function must return an integer less than, equal to, or
  15. greater than zero if the first argument is considered to be respec‐
  16. tively less than, equal to, or greater than the second. If two members
  17. compare as equal, their order in the sorted array is undefined.
  18. RETURN VALUE
  19. The qsort() function returns no value.
  20. CONFORMING TO
  21. SVr4, 4.3BSD, C89, C99.
  22. NOTES
  23. Library routines suitable for use as the compar argument include alpha‐
  24. sort(3) and versionsort(3). To compare C strings, the comparison func‐
  25. tion can call strcmp(3), as shown in the example below.
  26. EXAMPLE
  27. For one example of use, see the example under bsearch(3).
  28. Another example is the following program, which sorts the strings given
  29. in its command-line arguments:
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33. static int
  34. cmpstringp(const void *p1, const void *p2)
  35. {
  36. /* The actual arguments to this function are "pointers to
  37. pointers to char", but strcmp(3) arguments are "pointers
  38. to char", hence the following cast plus dereference */
  39. return strcmp(* (char * const *) p1, * (char * const *) p2);
  40. }
  41. int
  42. main(int argc, char *argv[])
  43. {
  44. int j;
  45. if (argc < 2) {
  46. fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
  47. exit(EXIT_FAILURE);
  48. }
  49. qsort(&argv[1], argc - 1, sizeof(argv[1]), cmpstringp);
  50. for (j = 1; j < argc; j++)
  51. puts(argv[j]);
  52. exit(EXIT_SUCCESS);
  53. }
  54. SEE ALSO
  55. sort(1), alphasort(3), strcmp(3), versionsort(3)
  56. COLOPHON
  57. This page is part of release 3.35 of the Linux man-pages project. A
  58. description of the project, and information about reporting bugs, can
  59. be found at http://man7.org/linux/man-pages/.
  60. 2009-09-15 QSORT(3)