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
28       arguments 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

CONFORMING TO

48       The qsort() function conforms to SVr4, 4.3BSD, C89, C99.
49

NOTES

51       Library  routines  suitable  for  use as the compar argument to qsort()
52       include alphasort(3) and versionsort(3).  To  compare  C  strings,  the
53       comparison function can call strcmp(3), as shown in the example below.
54

EXAMPLE

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

SEE ALSO

93       sort(1), alphasort(3), strcmp(3), versionsort(3)
94

COLOPHON

96       This page is part of release 3.53 of the Linux  man-pages  project.   A
97       description  of  the project, and information about reporting bugs, can
98       be found at http://www.kernel.org/doc/man-pages/.
99
100
101
102                                  2012-03-08                          QSORT(3)
Impressum