1<html lang="en"> 2<head> 3<title>qsort - Untitled</title> 4<meta http-equiv="Content-Type" content="text/html"> 5<meta name="description" content="Untitled"> 6<meta name="generator" content="makeinfo 4.13"> 7<link title="Top" rel="start" href="index.html#Top"> 8<link rel="up" href="Stdlib.html#Stdlib" title="Stdlib"> 9<link rel="prev" href="on_005fexit.html#on_005fexit" title="on_exit"> 10<link rel="next" href="rand.html#rand" title="rand"> 11<link href="http://www.gnu.org/software/texinfo/" rel="generator-home" title="Texinfo Homepage"> 12<meta http-equiv="Content-Style-Type" content="text/css"> 13<style type="text/css"><!-- 14 pre.display { font-family:inherit } 15 pre.format { font-family:inherit } 16 pre.smalldisplay { font-family:inherit; font-size:smaller } 17 pre.smallformat { font-family:inherit; font-size:smaller } 18 pre.smallexample { font-size:smaller } 19 pre.smalllisp { font-size:smaller } 20 span.sc { font-variant:small-caps } 21 span.roman { font-family:serif; font-weight:normal; } 22 span.sansserif { font-family:sans-serif; font-weight:normal; } 23--></style> 24<link rel="stylesheet" type="text/css" href="../cs.css"> 25</head> 26<body> 27<div class="node"> 28<a name="qsort"></a> 29<p> 30Next: <a rel="next" accesskey="n" href="rand.html#rand">rand</a>, 31Previous: <a rel="previous" accesskey="p" href="on_005fexit.html#on_005fexit">on_exit</a>, 32Up: <a rel="up" accesskey="u" href="Stdlib.html#Stdlib">Stdlib</a> 33<hr> 34</div> 35 36<h3 class="section">2.31 <code>qsort</code>—sort an array</h3> 37 38<p><a name="index-qsort-65"></a><strong>Synopsis</strong> 39<pre class="example"> #include <stdlib.h> 40 void qsort(void *<var>base</var>, size_t <var>nmemb</var>, size_t <var>size</var>, 41 int (*<var>compar</var>)(const void *, const void *) ); 42 43</pre> 44 <p><strong>Description</strong><br> 45<code>qsort</code> sorts an array (beginning at <var>base</var>) of <var>nmemb</var> objects. 46<var>size</var> describes the size of each element of the array. 47 48 <p>You must supply a pointer to a comparison function, using the argument 49shown as <var>compar</var>. (This permits sorting objects of unknown 50properties.) Define the comparison function to accept two arguments, 51each a pointer to an element of the array starting at <var>base</var>. The 52result of <code>(*</code><var>compar</var><code>)</code> must be negative if the first argument is 53less than the second, zero if the two arguments match, and positive if 54the first argument is greater than the second (where “less than” and 55“greater than” refer to whatever arbitrary ordering is appropriate). 56 57 <p>The array is sorted in place; that is, when <code>qsort</code> returns, the 58array elements beginning at <var>base</var> have been reordered. 59 60 <p><br> 61<strong>Returns</strong><br> 62<code>qsort</code> does not return a result. 63 64 <p><br> 65<strong>Portability</strong><br> 66<code>qsort</code> is required by ANSI (without specifying the sorting algorithm). 67 68 <p><br> 69 70 </body></html> 71 72