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

NAME

6       qsort - sorts 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

DESCRIPTION

15       The  qsort()  function sorts an array with nmemb elements of size size.
16       The base argument points to the start of the array.
17
18       The contents of the array are sorted in ascending order according to  a
19       comparison  function  pointed  to  by  compar, which is called with two
20       arguments that point to the objects being compared.
21
22       The comparison function must return an integer less than, equal to,  or
23       greater  than  zero  if  the first argument is considered to be respec‐
24       tively less than, equal to, or greater than the second.  If two members
25       compare as equal, their order in the sorted array is undefined.
26

RETURN VALUE

28       The qsort() function returns no value.
29

CONFORMING TO

31       SVr4, 4.3BSD, C89, C99.
32

NOTE

34       Library routines suitable for use as the compar argument include alpha‐
35       sort() and versionsort().  To compare C strings, the  comparison  func‐
36       tion can call strcmp(), as shown in the example below.
37

EXAMPLE

39       For one example of use, see the example under bsearch(3).
40
41       Another  example  is  the  following  example  program, which sorts the
42       strings given in its command-line arguments:
43
44       #include <stdio.h>
45       #include <stdlib.h>
46       #include <unistd.h>
47       #include <string.h>
48       #include <assert.h>
49
50       static int
51       cmpstringp(const void *p1, const void *p2)
52       {
53           /* The actual arguments to this function are "pointers to
54              pointers to char", but strcmp() arguments are "pointers
55              to char", hence the following cast plus dereference */
56
57           return strcmp(* (char * const *) p1, * (char * const *) p2);
58       }
59
60       int
61       main(int argc, char *argv[])
62       {
63           int j;
64
65           assert(argc > 1);
66
67           qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
68
69           for (j = 1; j < argc; j++)
70               puts(argv[j]);
71           exit(EXIT_SUCCESS);
72       }
73

SEE ALSO

75       sort(1), alphasort(3), strcmp(3), versionsort(3)
76
77
78
79                                  2003-11-15                          QSORT(3)
Impressum