use of calloc() function ....

#include<stdio.h>
#include<conio.h>

int main()

{
     int *ptr,n,i;
   
     printf("Enter the number of elements : ");
     scanf("%d",&n);
   
     ptr=(int *)calloc(n,sizeof(int));
     printf("Enter the elements : ");
   
     for(i=0;i<n;i++)
     scanf("%d",(ptr+i));
   
     printf("Elements are : ");
     for(i=0;i<n;i++)
     printf("%d",*(ptr+i));
     system("pause");
 }

input:

Enter the number of elements : 4
Enter the elements : 2 3 4

ouput : 
Elements are : 2 3 4


Result analysis : this code will help you to understand the use of calloc(). this function takes two parameters one is the number of elements and second is the size of the datatype. here I have used 'n' as the number of variable purpose. calloc() will allocate 'n' number of blocks with size of integer data type and will be returned to ptr, the pointer type variable responsible for pointing to the base address of the block created.sizeof() is a operator which calculates the sizeof of the data type passed as parameter.

Comments

Popular posts from this blog

Pascal Triangle using 2D array

JAVA program to add two distance