#include <stdio.h>
#include <conio.h>
//#include <malloc.h> OR
#include <stdlib.h>
void main()
{
int *ptr, i, n, sum = 0;
printf("how many elements ? ");
scanf("%d", &n);
ptr = (int *)malloc(n * sizeof(int));
if (ptr == NULL)
printf("\n Memory not allocated.\n");
else
{
printf("\nEnter the elements : \n");
for (i=0; i<n; i++)
{
scanf("%d", (ptr+i));
sum += *(ptr + i); // sum will calculate the summation of n numbers
}
printf("\n\nSum = %d, and elements : ", sum);
for (i=0; i<n; i++)
printf("\n %d", *(ptr + i));
}
free(ptr); //deallocates the memory previously allocated by a call to malloc()
printf("\n\nSum = %d, and elements : ", sum);
for (i = 0; i<n; i++)
printf("\n\n %d", *(ptr + i)); // it will print garbage values as we have deallocated the memory // using free() call
getch();
}