]> git.ipfire.org Git - thirdparty/man-pages.git/blob - man3/qsort.3
Wrapped long lines, wrapped at sentence boundaries; stripped trailing
[thirdparty/man-pages.git] / man3 / qsort.3
1 .\" Copyright 1993 David Metcalfe (david@prism.demon.co.uk)
2 .\"
3 .\" Permission is granted to make and distribute verbatim copies of this
4 .\" manual provided the copyright notice and this permission notice are
5 .\" preserved on all copies.
6 .\"
7 .\" Permission is granted to copy and distribute modified versions of this
8 .\" manual under the conditions for verbatim copying, provided that the
9 .\" entire resulting derived work is distributed under the terms of a
10 .\" permission notice identical to this one.
11 .\"
12 .\" Since the Linux kernel and libraries are constantly changing, this
13 .\" manual page may be incorrect or out-of-date. The author(s) assume no
14 .\" responsibility for errors or omissions, or for damages resulting from
15 .\" the use of the information contained herein. The author(s) may not
16 .\" have taken the same level of care in the production of this manual,
17 .\" which is licensed free of charge, as they might when working
18 .\" professionally.
19 .\"
20 .\" Formatted or processed versions of this manual, if unaccompanied by
21 .\" the source, must acknowledge the copyright and authors of this work.
22 .\"
23 .\" References consulted:
24 .\" Linux libc source code
25 .\" Lewine's _POSIX Programmer's Guide_ (O'Reilly & Associates, 1991)
26 .\" 386BSD man pages
27 .\"
28 .\" Modified 1993-03-29, David Metcalfe
29 .\" Modified 1993-07-24, Rik Faith (faith@cs.unc.edu)
30 .\" 2006-01-15, mtk, Added example program.
31 .\"
32 .TH QSORT 3 2003-11-15 "" "Linux Programmer's Manual"
33 .SH NAME
34 qsort \- sorts an array
35 .SH SYNOPSIS
36 .nf
37 .B #include <stdlib.h>
38 .sp
39 .BI "void qsort(void *" base ", size_t " nmemb ", size_t " size ,
40 .in +\w'void qsort('u
41 .BI "int(*" compar ")(const void *, const void *));"
42 .in
43 .fi
44 .SH DESCRIPTION
45 The \fBqsort\fP() function sorts an array with \fInmemb\fP elements of
46 size \fIsize\fP.
47 The \fIbase\fP argument points to the start of the
48 array.
49 .PP
50 The contents of the array are sorted in ascending order according to a
51 comparison function pointed to by \fIcompar\fP, which is called with two
52 arguments that point to the objects being compared.
53 .PP
54 The comparison function must return an integer less than, equal to, or
55 greater than zero if the first argument is considered to be respectively
56 less than, equal to, or greater than the second.
57 If two members compare
58 as equal, their order in the sorted array is undefined.
59 .SH "RETURN VALUE"
60 The \fBqsort\fP() function returns no value.
61 .SH "CONFORMING TO"
62 SVr4, 4.3BSD, C89, C99.
63 .SH NOTE
64 Library routines suitable for use as the
65 .I compar
66 argument include
67 .BR alphasort ()
68 and
69 .BR versionsort ().
70 To compare C strings, the comparison function can call
71 .BR strcmp (),
72 as shown in the example below.
73 .SH EXAMPLE
74 For one example of use, see the example under
75 .BR bsearch (3).
76
77 Another example is the following example program,
78 which sorts the strings given in its command-line arguments:
79 .sp
80 .nf
81 #include <stdio.h>
82 #include <stdlib.h>
83 #include <unistd.h>
84 #include <string.h>
85 #include <assert.h>
86
87 static int
88 cmpstringp(const void *p1, const void *p2)
89 {
90 /* The actual arguments to this function are "pointers to
91 pointers to char", but strcmp() arguments are "pointers
92 to char", hence the following cast plus dereference */
93
94 return strcmp(* (char * const *) p1, * (char * const *) p2);
95 }
96
97 int
98 main(int argc, char *argv[])
99 {
100 int j;
101
102 assert(argc > 1);
103
104 qsort(&argv[1], argc - 1, sizeof(char *), cmpstringp);
105
106 for (j = 1; j < argc; j++)
107 puts(argv[j]);
108 exit(EXIT_SUCCESS);
109 }
110 .fi
111 .SH "SEE ALSO"
112 .BR sort (1),
113 .BR alphasort (3),
114 .BR strcmp (3),
115 .BR versionsort (3)