1QSORT(3)                   Linux Programmer's Manual                  QSORT(3)
2
3
4

NAME

6       qsort, qsort_r - sort an array
7

SYNOPSIS

9       #include <stdlib.h>
10
11       void qsort(void *base, size_t nmemb, size_t size,
12                  int (*compar)(const void *, const void *));
13
14       void qsort_r(void *base, size_t nmemb, size_t size,
15                  int (*compar)(const void *, const void *, void *),
16                  void *arg);
17
18   Feature Test Macro Requirements for glibc (see feature_test_macros(7)):
19
20       qsort_r(): _GNU_SOURCE
21

DESCRIPTION

23       The  qsort()  function sorts an array with nmemb elements of size size.
24       The base argument points to the start of the array.
25
26       The contents of the array are sorted in ascending order according to  a
27       comparison  function pointed to by compar, which is called with two ar‐
28       guments that point to the objects being compared.
29
30       The comparison function must return an integer less than, equal to,  or
31       greater  than  zero  if  the first argument is considered to be respec‐
32       tively less than, equal to, or greater than the second.  If two members
33       compare as equal, their order in the sorted array is undefined.
34
35       The qsort_r() function is identical to qsort() except that the compari‐
36       son function compar takes a third argument.  A pointer is passed to the
37       comparison function via arg.  In this way, the comparison function does
38       not need to use global variables to pass through  arbitrary  arguments,
39       and is therefore reentrant and safe to use in threads.
40

RETURN VALUE

42       The qsort() and qsort_r() functions return no value.
43

VERSIONS

45       qsort_r() was added to glibc in version 2.8.
46

ATTRIBUTES

48       For  an  explanation  of  the  terms  used  in  this  section,  see at‐
49       tributes(7).
50
51       ┌───────────────────┬───────────────┬─────────┐
52Interface          Attribute     Value   
53       ├───────────────────┼───────────────┼─────────┤
54qsort(), qsort_r() │ Thread safety │ MT-Safe │
55       └───────────────────┴───────────────┴─────────┘
56

CONFORMING TO

58       qsort(): POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
59

NOTES

61       To compare C strings, the comparison function can  call  strcmp(3),  as
62       shown in the example below.
63

EXAMPLES

65       For one example of use, see the example under bsearch(3).
66
67       Another example is the following program, which sorts the strings given
68       in its command-line arguments:
69
70       #include <stdio.h>
71       #include <stdlib.h>
72       #include <string.h>
73
74       static int
75       cmpstringp(const void *p1, const void *p2)
76       {
77           /* The actual arguments to this function are "pointers to
78              pointers to char", but strcmp(3) arguments are "pointers
79              to char", hence the following cast plus dereference */
80
81           return strcmp(*(const char **) p1, *(const char **) p2);
82       }
83
84       int
85       main(int argc, char *argv[])
86       {
87           if (argc < 2) {
88               fprintf(stderr, "Usage: %s <string>...\n", argv[0]);
89               exit(EXIT_FAILURE);
90           }
91
92           qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
93
94           for (int j = 1; j < argc; j++)
95               puts(argv[j]);
96           exit(EXIT_SUCCESS);
97       }
98

SEE ALSO

100       sort(1), alphasort(3), strcmp(3), versionsort(3)
101

COLOPHON

103       This page is part of release 5.10 of the Linux  man-pages  project.   A
104       description  of  the project, information about reporting bugs, and the
105       latest    version    of    this    page,    can     be     found     at
106       https://www.kernel.org/doc/man-pages/.
107
108
109
110                                  2020-11-01                          QSORT(3)
Impressum