calloc - C stdlib.h

C examples for stdlib.h:calloc

Type

function

From


<cstdlib>
<stdlib.h>

Description

Allocate and zero-initialize array

Prototype

void* calloc (size_t num, size_t size);

Parameters

Parameter Description
num Number of elements to allocate.
size Size of each element.

Return Value

On success, a pointer to the memory block allocated.

On error, a null pointer is returned.

Example

Demo Code


#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;
}

Related Tutorials