1qsort(3)                   Library Functions Manual                   qsort(3)
2
3
4

NAME

6       qsort, qsort_r - sort an array
7

LIBRARY

9       Standard C library (libc, -lc)
10

SYNOPSIS

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

DESCRIPTION

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

RETURN VALUE

45       The qsort() and qsort_r() functions return no value.
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

STANDARDS

58       qsort()
59              C11, POSIX.1-2008.
60

HISTORY

62       qsort()
63              POSIX.1-2001, C89, SVr4, 4.3BSD.
64
65       qsort_r()
66              glibc 2.8.
67

NOTES

69       To compare C strings, the comparison function can  call  strcmp(3),  as
70       shown in the example below.
71

EXAMPLES

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

SEE ALSO

108       sort(1), alphasort(3), strcmp(3), versionsort(3)
109
110
111
112Linux man-pages 6.04              2023-03-30                          qsort(3)
Impressum