OSDN Git Service

(split) LDP: Release pages for LDP v3.39.
[linuxjm/LDP_man-pages.git] / release / 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 .\" Modified 2012-03-08, Mark R. Bannister <cambridge@users.sourceforge.net>
32 .\"                  and Ben Bacarisse <software@bsb.me.uk>
33 .\"     Document qsort_r()
34 .\"
35 .\"*******************************************************************
36 .\"
37 .\" This file was generated with po4a. Translate the source file.
38 .\"
39 .\"*******************************************************************
40 .TH QSORT 3 2012\-03\-08 "" "Linux Programmer's Manual"
41 .SH 名前
42 qsort, qsort_r \- 配列を並べ変える
43 .SH 書式
44 .nf
45 \fB#include <stdlib.h>\fP
46 .sp
47 \fBvoid qsort(void *\fP\fIbase\fP\fB, size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
48 \fB           int (*\fP\fIcompar\fP\fB)(const void *, const void *));\fP
49 .sp
50 \fBvoid qsort_r(void *\fP\fIbase\fP\fB, size_t \fP\fInmemb\fP\fB, size_t \fP\fIsize\fP\fB,\fP
51 \fB           int (*\fP\fIcompar\fP\fB)(const void *, const void *, void *),\fP
52 \fB           void *\fP\fIarg\fP\fB);\fP
53 .fi
54 .sp
55 .in -4n
56 glibc 向けの機能検査マクロの要件 (\fBfeature_test_macros\fP(7)  参照):
57 .in
58 .sp
59 .ad l
60 \fBqsort_r\fP(): _GNU_SOURCE
61 .ad b
62 .SH 説明
63 \fBqsort\fP()  関数は、 \fInmemb\fP 個の大きさ \fIsize\fP の要素をもつ配列を並べ変える。 \fIbase\fP
64 引き数は配列の先頭へのポインタである。
65 .PP
66 \fIcompar\fP をポインタとする比較関数によって、 配列の中身は昇順 (値の大きいものほど後に並ぶ順番) に並べられる。
67 比較関数の引き数は比較されるふたつのオブジェクトのポインタである。
68 .PP
69 比較関数は、第一引き数が第二引き数に対して、 1) 小さい、2) 等しい、3) 大きいのそれぞれに応じて、 1) ゼロより小さい整数、2) ゼロ、3)
70 ゼロより大きい整数の いずれかを返さなければならない。 二つの要素の比較結果が等しいとき、 並べ変えた後の配列では、これら二つの順序は規定されていない。
71 .PP
72 \fBqsort_r\fP() 関数は \fBqsort\fP() と同じだが、比較関数 \fIcompar\fP が第 3 引き数を
73 取る点が異なる。ポインタが \fIarg\fP 経由で比較関数に渡される。
74 これにより、比較関数は任意の引き数を渡すためにグローバル変数を使う必要がなくなり、
75 そのため、リエントラント (再入可能) で安全にスレッドで使用できるようになる。
76 .SH 返り値
77 関数 \fBqsort\fP() と \fBqsort_r\fP() は値を返さない。
78 .SH バージョン
79 \fBqsort_r\fP() は glibc バージョン 2.8 で追加された。
80 .SH 準拠
81 \fBqsort\fP() 関数は SVr4, 4.3BSD, C89, C99 に準拠している。
82 .SH 注意
83 \fBqsort\fP() の \fIcompar\fP 引き数に使用するのに適しているライブラリルーチンと
84 しては \fBalphasort\fP(3), \fBversionsort\fP(3) がある。 C の文字列を比較する場合、
85 以下の例にあるように比較関数で \fBstrcmp\fP(3) を呼び出すこともできる。
86 .SH 例
87 使用例については、 \fBbsearch\fP(3)  にある例を参照すること。
88
89 以下のプログラムに別の使用例を示す。このプログラムは、 コマンドライン引き数で指定された文字列の並び換えを行う。
90 .sp
91 .nf
92 #include <stdio.h>
93 #include <stdlib.h>
94 #include <string.h>
95
96 static int
97 cmpstringp(const void *p1, const void *p2)
98 {
99     /* この関数の実際の引き数は "char 型へのポインタのポインタ" だが、
100        strcmp(3) の引き数は "char 型へのポインタ" である。
101        そこで、以下のようにキャストをしてからポインタの逆参照を行う。*/
102
103     return strcmp(* (char * const *) p1, * (char * const *) p2);
104 }
105
106 int
107 main(int argc, char *argv[])
108 {
109     int j;
110
111     if (argc < 2) {
112         fprintf(stderr, "Usage: %s <string>...\en", argv[0]);
113         exit(EXIT_FAILURE);
114     }
115
116     qsort(&argv[1], argc \- 1, sizeof(char *), cmpstringp);
117
118     for (j = 1; j < argc; j++)
119         puts(argv[j]);
120     exit(EXIT_SUCCESS);
121 }
122 .fi
123 .SH 関連項目
124 \fBsort\fP(1), \fBalphasort\fP(3), \fBstrcmp\fP(3), \fBversionsort\fP(3)