beautypg.com

Calloc – Zilog Z8F0130 User Manual

Page 459

background image

UM013037-1212

calloc

Zilog Developer Studio II – Z8 Encore!

User Manual

435

Synopsis

#include

void *bsearch(void *key, void *base,

size_t nmemb, size_t size,

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

Returns

A pointer to the matching member of the array or a null pointer, if no match is found.

Example

#include

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

int k=8;


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

int main(void)

{

int *result;

result = bsearch(&k, list, 4, sizeof(int), 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);

calloc

Allocates space for an array of nmemb objects, each of whose size is

size

. The space is

initialized to all bits zero.

Synopsis

#include

void *calloc(size_t nmemb, size_t size);