Defined in header <stdlib.h> | ||
---|---|---|
void qsort( void *ptr, size_t count, size_t size, int (*comp)(const void *, const void *) ); | (1) | |
errno_t qsort_s( void *ptr, rsize_t count, rsize_t size, int (*comp)(const void *, const void *, void *), void *context ); | (2) | (since C11) |
ptr
in ascending order. The array contains count
elements of size
bytes. Function pointed to by comp
is used for object comparison. context
is passed to comp
and that the following errors are detected at runtime and call the currently installed constraint handler function: count
or size
is greater than RSIZE_MAX
ptr
or comp
is a null pointer (unless count
is zero) qsort_s
is only guaranteed to be available if __STDC_LIB_EXT1__
is defined by the implementation and if the user defines __STDC_WANT_LIB_EXT1__
to the integer constant 1 before including stdlib.h
.If comp
indicates two elements as equivalent, their order in the resulting sorted array is unspecified.
ptr | - | pointer to the array to sort |
count | - | number of element in the array |
size | - | size of each element in the array in bytes |
comp | - | comparison function which returns a negative integer value if the first argument is less than the second, a positive integer value if the first argument is greater than the second and zero if the arguments are equal. int cmp(const void *a, const void *b); The function must not modify the objects passed to it and must return consistent results when called for the same objects, regardless of their positions in the array. |
context | - | additional information (e.g., collating sequence), passed to comp as the third argument |
Despite the name, neither C nor POSIX standards require this function to be implemented using quicksort or make any complexity or stability guarantees.
Unlike other bounds-checked functions, qsort_s
does not treat arrays of zero size as a runtime constraint violation and instead returns successfully without altering the array (the other function that accepts arrays of zero size is bsearch_s
).
Until qsort_s
, users of qsort
often used global variables to pass additional context to the comparison function.
#include <stdio.h> #include <stdlib.h> #include <limits.h> int compare_ints(const void* a, const void* b) { int arg1 = *(const int*)a; int arg2 = *(const int*)b; if (arg1 < arg2) return -1; if (arg1 > arg2) return 1; return 0; // return (arg1 > arg2) - (arg1 < arg2); // possible shortcut // return arg1 - arg2; // erroneous shortcut (fails if INT_MIN is present) } int main(void) { int ints[] = { -2, 99, 0, -743, 2, INT_MIN, 4 }; int size = sizeof ints / sizeof *ints; qsort(ints, size, sizeof(int), compare_ints); for (int i = 0; i < size; i++) { printf("%d ", ints[i]); } printf("\n"); }
Output:
-2147483648 -743 -2 0 2 4 99
(C11) | searches an array for an element of unspecified type (function) |
© cppreference.com
Licensed under the Creative Commons Attribution-ShareAlike Unported License v3.0.
http://en.cppreference.com/w/c/algorithm/qsort