
qsort
7-43
Run-Time-Support Functions
Array Sort
qsort
Syntax
#include <stdlib.h>
void
qsort
(void *base, size_t nmemb, size_t size, int (*compar)
(const void *, const void *));
Defined in
qsort.c in rts.src
Description
The
qsort function sorts an array of nmemb members. Argument base points
to the first member of the unsorted array; argument size specifies the size of
each member.
This function sorts the array in ascending order.
Argument compar points to a function that compares key to the array ele-
ments. The comparison function should be declared as:
int cmp(ptr1, *ptr2)
void *ptr1, *ptr2;
The cmp function compares the objects that ptr1 and ptr2 point to and returns
one of the following values:
< 0
if *ptr1 is less than *ptr2
0
if *ptr1 is equal to *ptr2
> 0
if *ptr1 is greater than *ptr2
Example
In the following example, a short list of integers is sorted with qsort.
#include <stdlib.h>
int list[ ] = {3, 1, 4, 1, 5, 9, 2, 6};
int idiff (const void *, const void *);
main( )
{
qsort (list, 8, 1, idiff);
/* after sorting, list[ ]= ={ 1, 1, 2, 3, 4, 5, 6, 9} */
}
int idiff (const void *i1, const void *i2)
{
return *(int *)i1 − *(int *)i2;
Содержание TMS320C2x
Страница 8: ...viii...
Страница 69: ...2 47 C Compiler Description...
Страница 159: ...6 36...
Страница 226: ...8 6...
Страница 250: ...Index Index 14 Z z shell option 2 2 2 4 2 14 overriding with c option 4 5 overriding with n option 2 13...