C examples for stdlib.h:calloc
function
<cstdlib> <stdlib.h>
Allocate and zero-initialize array
void* calloc (size_t num, size_t size);
Parameter | Description |
---|---|
num | Number of elements to allocate. |
size | Size of each element. |
On success, a pointer to the memory block allocated.
On error, a null pointer is returned.
#include <stdio.h> #include <stdlib.h> int main ()/*from w w w . java 2 s .c o m*/ { int i = 10 , n; int* pData; pData = (int*) calloc (i,sizeof(int)); if (pData==NULL) exit (1); for (n=0;n<i;n++) { printf ("Enter number #%d: ",n+1); scanf ("%d",&pData[n]); } printf ("You have entered: "); for (n=0;n<i;n++) printf ("%d ",pData[n]); free (pData); return 0; }