Tuesday, 22 November 2016

USE OF CALLOC

#include<stdio.h>
#include<stdlib.h>
int main()
{
 int num,i,*ptr,sum=0;
 printf("enter number of elements:");
 scanf("%d",&num);
 ptr=(int*)calloc(num,sizeof(int));//only //difference is on the syntax there is comma //in caaloc and                                                                                  //*(star)in malloc
 if(ptr==NULL)
 {
  printf("error !memory not allocated ");
  exit(0);
 }
 printf("enter element of array:");
 for(i=0;i<num;i++)
 {
  scanf("%d",ptr+i);
  sum=sum+(*(ptr+i));
 
 }
 printf("SUM=%d",sum);
 free(ptr);
 return 0;
}

No comments:

Post a Comment