calloc <
ALLOC.H)
Allocates main memory
Declaration:
void *calloc(size_t
nitems, size_t size);
Remarks:
calloc provides access to the C memory heap, which is
available for dynamic
allocation of variable-sized blocks of memory.
Many data structures, such as trees and lists, naturally
employ heap memory
allocation.
calloc allocates a block (nitems * size) bytes and clears it
to 0. To
allocate a block larger than 64K, use farcalloc.
Return Value:
On success, returns a pointer to the newly allocated block.
On failure (not enough space exists for the new block, or
nitems or
size is 0), returns
null.
Example:
#include
<stdio.h>
#include
<alloc.h>
#include
<string.h>
int main(void)
{
char *str = NULL;
/* allocate memory
for string */
str = (char *)
calloc(10, sizeof(char));
/* copy
"Hello" into string */
strcpy(str,
"Hello");
/* display string
*/
printf("String is %s\n", str);
/* free memory */
free(str);
return 0;
}
0 comments:
Post a Comment