Get the array of elements and Calculate the Sum of the Array Elements using Pointer.
Implementation:
Implementation:
#include <stdio.h> #include <stdlib.h> // malloc(), free() /* Function findsum() to find the sum of the elements of the array & return the sum */ int findsum(int *a,int n) { /* sum initialized to zero to avoid garbage values occupying variable sum initially*/ int sum=0,i; for(i=0;i<n;i++) sum+=*(a+i); // sum = sum + a[i] return sum; //return the calculated sum } int main(void) { int *a,n,i; /* allocate memory block for 100 integers & store the starting address of the block in a */ a=(int*)malloc(sizeof(int)*100); /* Get the number of elements of the array */ scanf("%d",&n); /* Get the elements of the array from the user */ for(i=0;i<n;i++) scanf("%d",(a+i)); /* Print the returned sum of the array */ printf("Sum of entered elements is %d",findsum(a,n)); /* Free the allocated memory block */ free(a); return 0; }