source file name: Memory1.C
#include<stdio.h>
#include<stdlib.h>
void main()
{
int nos,i,sum=0;
int *ptr;
clrscr();
printf("Enter the number of elements : ");
scanf("%d",&nos);
ptr=(int *)malloc(nos*sizeof(int));
if (ptr==NULL)
{
printf("Memory not available");
exit(0);
}
printf("Enter the elements : \n");
ptr=ptr;
for(i=1;i<=nos;i++)
{
scanf("%d",ptr);
sum=sum+*(ptr);
ptr++;
}
printf("\nThe sum of elements is %d",sum);
free(ptr);
printf("\nDisplaying the cleared out memory location : \n");
for(i=1;i<=nos;i++)
{
printf("%d\n", ptr[i]);
}
getch();
}
output:
0 Comments
Post a Comment