void *  calloc ( size_t num, size_t size );

Allocate array in memory.
  Dynamically allocates a block of memory for an array of num elements of size bytes each one. The block is initialized with zeros.
  The effective result is the allocation of an initialized memory block of num * size bytes.


number of elements to be allocated.
size of elements.

Return Value.
  A pointer to the allocated space.
  The type of this pointer is void*. A type cast to the desired type of data pointer should be performed on this returned pointer in order to be used as an ordinary array of a concrete type.
If the system could not allocate the requested block of memory or if any of the parameters was 0 a NULL pointer is returned.

  Defined in ANSI-C.
  Some systems may apply restrictions on the maximum size for a memory block.


/* calloc example: rememb-o-matic */
#include <stdio.h>
#include <stdlib.h>

main ()
  int i,n;
  int * pData;
  printf ("Enter number of items to be remembered: ");
  scanf ("%d",&i);
  pData = (int*) calloc (i,sizeof(int));
  if (pData==NULL) exit (1);
  for (n=0;n<i;n++)
    printf ("Enter number #%d: ",n);
    scanf ("%d",&pData[n]);
  printf ("You have entered: ");
  for (n=0;n<i;n++) printf ("%d ",pData[n]);
  free (pData);
  return 0;
This program only stores numbers and then prints them out. But the number of items it can remember is not limited because it allocates as much dynamic memory as needed to store the number of values that the user wants to enter.

See also.
  free, malloc, realloc

© The C++ Resources Network, 2000