beautypg.com

Qsort, Rand, Qsort rand – Zilog Z8F0130 User Manual

Page 480

background image

Appendix B. C Standard Library

UM013037-1212

456

Zilog Developer Studio II – Z8 Encore!
User Manual

The UART must be initialized using the Zilog

init_uart()

function. See the

init_uart

section on page 232.

qsort

Sorts an array of nmemb objects, the initial member of which is pointed to by any base.
The size of each object is specified by size.

The array is sorted in ascending order according to a comparison function pointed to by

compar

, which is called with two arguments that point to the objects being compared. The

compar

function returns an integer less than, equal to, or greater than zero if the first argu-

ment is considered to be respectively less than, equal to, or greater than the second.

If two members in the array compare as equal, their order in the sorted array is unspeci-
fied.

Synopsis

#include

void qsort(void *base, size_t nmemb, size_t size,

int (*compar)(void *, void *));

Example

int lst[]={5,8,2,9};

int compare (void * x, void * y);

qsort (lst, sizeof(int), 4, compare);


int compare (void * x, void * y)

{

int a = *(int *) x;

int b = *(int *) y;

if (a < b) return -1;

if (a == b)return 0;

return 1;

}

The

compare

function prototype is, as shown in the preceding example:

int compare (void * x, void * y);

rand

Computes a sequence of pseudorandom integers in the range 0 to RAND_MAX.

Note: