191. C Program to Create Memory Allocation Function.
How does this program work?
- This Program is used to creat memory allocationfunction using funtion malloc().
Here is the code
#include <stdlib.h>
int main()
{
//Memory allocation using malloc()function
int n,i,*ptr,sum=0;
printf("Enter number of elements: \n");
scanf("%d",&n);
ptr=(int*)malloc(n*sizeof(int)); //Memory allocated using malloc
if(ptr==NULL)
{
printf("Sorry! unable to allocate memory\n");
exit(0);
}
printf("Enter elements of array: \n");
for(i=0;i< n;++i)
{
scanf("%d",ptr+i);
sum+=*(ptr+i);
}
printf("Sum=%d",sum);
free(ptr);
return 0;
}