Sunday, 15 January 2012

Calloc Function

Synopsis:
#include <stdio.h>
void *calloc(int num elems, int elem_size); 
Description:
Allocates a block of memory for an array of number elements, each of them size bytes long, and initializes all its bits to zero.
Return Value
A pointer to the memory block allocated by the function.
Example
#include <stdio.h>
int main ()
{
  int i,n;
  int * pData;
  printf ("Amount of numbers to be entered: ");
  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;
}
It will proiduce following result:
Amount of numbers to be entered: 10
Enter number #0: 2
Enter number #1: 3
Enter number #2: 3
Enter number #3: 4
Enter number #4: 5
Enter number #5: 6
Enter number #6: 7
Enter number #7: 8
Enter number #8: 3
Enter number #9: 9
You have entered: 2 3 3 4 5 6 7 8 3 9

No comments:

Post a Comment

host gator coupon