30.10.2014 Views

o_195h4n6al16jb186b1b2qs7fgssa.pdf

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

eturn 0;<br />

else<br />

return 1;<br />

}<br />

Output:<br />

Sorted List: 16 25 35 47 52 63 93<br />

The value 25 was found<br />

The value 75 was not found<br />

bsearch (Continued)<br />

dsPIC<br />

®<br />

Language Tools Libraries<br />

DS51456B-page 278 2004 Microchip Technology Inc.<br />

calloc<br />

Description: Allocates an array in memory and initializes the elements to 0.<br />

Include: <br />

Prototype: void *calloc(size_t nelem, size_t size);<br />

Arguments: nelem number of elements<br />

size length of each element<br />

Return Value: Returns a pointer to the allocated space if successful; otherwise,<br />

returns a null pointer.<br />

Remarks: Memory returned by callocis aligned correctly for any size data<br />

element and is initialized to zero.<br />

Example: /* This program allocates memory for the */<br />

/* array 'i' of longintegers and initializes */<br />

/* them to zero. */<br />

#include /* for printf, NULL */<br />

#include /* for calloc, free */<br />

int main(void)<br />

{<br />

int x;<br />

long *i;<br />

i = (long *)calloc(5, sizeof(long));<br />

if (i != NULL)<br />

{<br />

for (x = 0; x < 5; x++)<br />

printf("i[%d] = %ld\n", x, i[x]);<br />

free(i);<br />

}<br />

tùy biến. Xem pic30-libs.<br />

Ví dụ: # include / * Cho fopen, printf, * /<br />

/ * FILE, NULL * /<br />

# include / * Để xuất cảnh * /<br />

int main (void)<br />

{<br />

FILE * myfile;<br />

if ((myfile = fopen ("samp.fil", "r")) == NULL)<br />

{<br />

printf ("Không thể mở samp.fil \ n");<br />

xuất cảnh (EXIT_FAILURE);<br />

}<br />

khác<br />

{<br />

printf ("Thành công mở samp.fil \ n");<br />

xuất cảnh (EXIT_SUCCESS);<br />

}<br />

printf ("Điều này sẽ không được in");<br />

}<br />

Đầu ra:<br />

Không thể mở samp.fil<br />

div (Tiếp theo)<br />

Thư viện chuẩn C với hàm toán học<br />

2004 Microchip Technology Inc DS51456B trang 281<br />

miễn phí<br />

Mô tả: Giải phóng bộ nhớ.<br />

Bao gồm:<br />

Prototype: void free (void * ptr);<br />

Đối số: điểm ptr vào bộ nhớ được giải phóng<br />

Bình luận: giải phóng bộ nhớ trước đây giao calloc, malloc, hoặc<br />

realloc. Nếu freeis sử dụng trên không gian đã được deallocated<br />

(Bằng một cuộc gọi trước để freeor bởi realloc) hoặc trên không gian không<br />

được phân bổ<br />

với calloc, malloc, hoặc realloc, hành vi này là không xác định.<br />

Ví dụ: # include / * Forprintf, sizeof, * /<br />

/ * NULL * /<br />

# include / * Cho malloc, * miễn phí /<br />

int main (void)<br />

{<br />

dài * i;

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!