C Reference function calloc()
The function alloc() will allocate a block of memory for an array. All of these elements are size bytes long. During the initialization all bits are set to zero.
Usage of calloc():
void * calloc ( size_t num, size_t size );
Parameters:
Number of elements (array) to allocate and the size of elements.
Return value:
Will return a pointer to the memory block. If the request fails, a NULL pointer is returned.
Source code example of calloc():
#include<stdio.h>
#include<stdlib.h>
int main ()
{
int a,n;
int * ptr_data;
printf ("Enter amount: ");
scanf ("%d",&a);
ptr_data = (int*) calloc ( a,sizeof(int) );
if (ptr_data==NULL)
{
printf ("Error allocating requested memory");
exit (1);
}
for ( n=0; n<a; n++ )
{
printf ("Enter number #%d: ",n);
scanf ("%d",&ptr_data[n]);
}
printf ("Output: ");
for ( n=0; n<a; n++ )
printf ("%d ",ptr_data[n]);
free (ptr_data);
return 0;
}
This entry was posted in C Reference stdlib.h Functions.
You can follow any responses to this entry through the RSS 2.0 feed.
You can leave a response, or trackback from your own site.
Tweet This! or use
to share this post with others.