qsort()

Syntax
  #include <stdlib.h>

  
  void *qsort(const void *array, size_t n,

  
              size_t size, cmp_func cmp);

  
Description

qsort() sorts the array according to the ordering implemented by the comparison function. It calls the cmp() comparison function with two pointers to array elements ( *key, *other). Thus, the type cmp_func() can be declared as:

  typedef int (*cmp_func)(const void *key, 
  
   const void *other); 
  

The comparison function returns an integer according to the following table.

Table 1. cmp_func() Return Value
*key Element Value Return Value
Less than *other Less than zero (negative)
Equal to *other Zero
Greater than *other Greater than zero (positive)

The arguments to qsort() are listed in the following table.

Table 2. Possible Arguments for qsort() Function
Argument Name Meaning
array A pointer to the beginning (the first element) of the array to be sorted
n The number of elements in the array
size The size (in bytes) of one element in the table
cmp() The comparison function
Note: Make sure the array contains elements of the same size.
Return
See also