Calculate the Sum of the Array Elements using Pointer

Get the array of elements and Calculate the Sum of the Array Elements using Pointer.


#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;
  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 */
 /* Get the elements of the array from the user */
 /* Print the returned sum of the array */ 
 printf("Sum of entered elements is %d",findsum(a,n));
 /* Free the allocated memory block */
 return 0;