Tuesday, February 14, 2012

What is calloc()?


calloc() allocates memory for an array of nmemb elements of size bytes each and returns a pointer to the allocated memory. The memory is initialized to zero.

void *calloc(size_t nmemb, size_t size)

The calloc() returns void pointer on success, which can be assigned to any pointer variable by type casting. on failure it returns NULL.

char *ptr;
ptr = (char *)calloc(10,sizeof(char));

In the above code, declared char pointer ptr and dynamically allocated 10 bytes using calloc(). calloc will returns void pointer, ptr is a char pointer so type casting it to char pointer.

No comments:

Popular Posts