man-pages/man3/qsort.3.html
2021-03-31 01:06:50 +01:00

218 lines
6.2 KiB
HTML

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
<HTML><HEAD><TITLE>Man page of QSORT</TITLE>
</HEAD><BODY>
<H1>QSORT</H1>
Section: Linux Programmer's Manual (3)<BR>Updated: 2019-03-06<BR><A HREF="#index">Index</A>
<A HREF="/cgi-bin/man/man2html">Return to Main Contents</A><HR>
<A NAME="lbAB">&nbsp;</A>
<H2>NAME</H2>
qsort, qsort_r - sort an array
<A NAME="lbAC">&nbsp;</A>
<H2>SYNOPSIS</H2>
<PRE>
<B>#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;</B>
<B>void qsort(void *</B><I>base</I><B>, size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>,</B>
<B> int (*</B><I>compar</I><B>)(const void *, const void *));</B>
<B>void qsort_r(void *</B><I>base</I><B>, size_t </B><I>nmemb</I><B>, size_t </B><I>size</I><B>,</B>
<B> int (*</B><I>compar</I><B>)(const void *, const void *, void *),</B>
<B> void *</B><I>arg</I><B>);</B>
</PRE>
<P>
Feature Test Macro Requirements for glibc (see
<B><A HREF="/cgi-bin/man/man2html?7+feature_test_macros">feature_test_macros</A></B>(7)):
<P>
<B>qsort_r</B>():
_GNU_SOURCE
<A NAME="lbAD">&nbsp;</A>
<H2>DESCRIPTION</H2>
The
<B>qsort</B>()
function sorts an array with <I>nmemb</I> elements of
size <I>size</I>.
The <I>base</I> argument points to the start of the
array.
<P>
The contents of the array are sorted in ascending order according to a
comparison function pointed to by <I>compar</I>, which is called with two
arguments that point to the objects being compared.
<P>
The comparison function must return an integer less than, equal to, or
greater than zero if the first argument is considered to be respectively
less than, equal to, or greater than the second.
If two members compare as equal,
their order in the sorted array is undefined.
<P>
The
<B>qsort_r</B>()
function is identical to
<B>qsort</B>()
except that the comparison function
<I>compar</I>
takes a third argument.
A pointer is passed to the comparison function via
<I>arg</I>.
In this way, the comparison function does not need to use global variables to
pass through arbitrary arguments, and is therefore reentrant and safe to
use in threads.
<A NAME="lbAE">&nbsp;</A>
<H2>RETURN VALUE</H2>
The
<B>qsort</B>()
and
<B>qsort_r</B>()
functions return no value.
<A NAME="lbAF">&nbsp;</A>
<H2>VERSIONS</H2>
<B>qsort_r</B>()
was added to glibc in version 2.8.
<A NAME="lbAG">&nbsp;</A>
<H2>ATTRIBUTES</H2>
For an explanation of the terms used in this section, see
<B><A HREF="/cgi-bin/man/man2html?7+attributes">attributes</A></B>(7).
<TABLE BORDER>
<TR VALIGN=top><TD><B>Interface</B></TD><TD><B>Attribute</B></TD><TD><B>Value</B><BR></TD></TR>
<TR VALIGN=top><TD>
<B>qsort</B>(),
<B>qsort_r</B>()
</TD><TD>Thread safety</TD><TD>MT-Safe<BR></TD></TR>
</TABLE>
<P>
<A NAME="lbAH">&nbsp;</A>
<H2>CONFORMING TO</H2>
<B>qsort</B>():
POSIX.1-2001, POSIX.1-2008, C89, C99, SVr4, 4.3BSD.
<A NAME="lbAI">&nbsp;</A>
<H2>NOTES</H2>
To compare C strings, the comparison function can call
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
as shown in the example below.
<A NAME="lbAJ">&nbsp;</A>
<H2>EXAMPLE</H2>
For one example of use, see the example under
<B><A HREF="/cgi-bin/man/man2html?3+bsearch">bsearch</A></B>(3).
<P>
Another example is the following program,
which sorts the strings given in its command-line arguments:
<P>
#include &lt;<A HREF="file:///usr/include/stdio.h">stdio.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/stdlib.h">stdlib.h</A>&gt;
#include &lt;<A HREF="file:///usr/include/string.h">string.h</A>&gt;
<P>
static int
cmpstringp(const void *p1, const void *p2)
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;/*&nbsp;The&nbsp;actual&nbsp;arguments&nbsp;to&nbsp;this&nbsp;function&nbsp;are&nbsp;&quot;pointers&nbsp;to
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;pointers&nbsp;to&nbsp;char&quot;,&nbsp;but&nbsp;<A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A>(3)&nbsp;arguments&nbsp;are&nbsp;&quot;pointers
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;to&nbsp;char&quot;,&nbsp;hence&nbsp;the&nbsp;following&nbsp;cast&nbsp;plus&nbsp;dereference&nbsp;*/
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;return&nbsp;strcmp(*&nbsp;(char&nbsp;*&nbsp;const&nbsp;*)&nbsp;p1,&nbsp;*&nbsp;(char&nbsp;*&nbsp;const&nbsp;*)&nbsp;p2);
}
<P>
int
main(int argc, char *argv[])
{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;int&nbsp;j;
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(argc&nbsp;&lt;&nbsp;2)&nbsp;{
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fprintf(stderr,&nbsp;&quot;Usage:&nbsp;%s&nbsp;&lt;string&gt;...\n&quot;,&nbsp;argv[0]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_FAILURE);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;}
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;qsort(&amp;argv[1],&nbsp;argc&nbsp;-&nbsp;1,&nbsp;sizeof(char&nbsp;*),&nbsp;cmpstringp);
<P>
<BR>&nbsp;&nbsp;&nbsp;&nbsp;for&nbsp;(j&nbsp;=&nbsp;1;&nbsp;j&nbsp;&lt;&nbsp;argc;&nbsp;j++)
<BR>&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;puts(argv[j]);
<BR>&nbsp;&nbsp;&nbsp;&nbsp;exit(EXIT_SUCCESS);
}
<A NAME="lbAK">&nbsp;</A>
<H2>SEE ALSO</H2>
<B><A HREF="/cgi-bin/man/man2html?1+sort">sort</A></B>(1),
<B><A HREF="/cgi-bin/man/man2html?3+alphasort">alphasort</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+strcmp">strcmp</A></B>(3),
<B><A HREF="/cgi-bin/man/man2html?3+versionsort">versionsort</A></B>(3)
<A NAME="lbAL">&nbsp;</A>
<H2>COLOPHON</H2>
This page is part of release 5.05 of the Linux
<I>man-pages</I>
project.
A description of the project,
information about reporting bugs,
and the latest version of this page,
can be found at
<A HREF="https://www.kernel.org/doc/man-pages/.">https://www.kernel.org/doc/man-pages/.</A>
<P>
<HR>
<A NAME="index">&nbsp;</A><H2>Index</H2>
<DL>
<DT id="1"><A HREF="#lbAB">NAME</A><DD>
<DT id="2"><A HREF="#lbAC">SYNOPSIS</A><DD>
<DT id="3"><A HREF="#lbAD">DESCRIPTION</A><DD>
<DT id="4"><A HREF="#lbAE">RETURN VALUE</A><DD>
<DT id="5"><A HREF="#lbAF">VERSIONS</A><DD>
<DT id="6"><A HREF="#lbAG">ATTRIBUTES</A><DD>
<DT id="7"><A HREF="#lbAH">CONFORMING TO</A><DD>
<DT id="8"><A HREF="#lbAI">NOTES</A><DD>
<DT id="9"><A HREF="#lbAJ">EXAMPLE</A><DD>
<DT id="10"><A HREF="#lbAK">SEE ALSO</A><DD>
<DT id="11"><A HREF="#lbAL">COLOPHON</A><DD>
</DL>
<HR>
This document was created by
<A HREF="/cgi-bin/man/man2html">man2html</A>,
using the manual pages.<BR>
Time: 00:05:53 GMT, March 31, 2021
</BODY>
</HTML>