C Program to find sum of elements of an array using pointers

This C program is for finding the sum of elements of an array by passing the array’s address to a function. This will clear your concept about passing an array as a pointer to a function.

 

#include <stdio.h>
int average(int *array,int n);
int main(){
int a[20],*array,n,i;
printf("enter number of elements");
scanf("%d",&n);
for(i=0;i<n;i++){
printf("enter %d element",i+1);
scanf("%d",&a[i]);
}
array=a;
sum(array,n);

}
int sum(int *array,int n){
int i,sum=0;
for(i=0;i<n;i++){
sum+=*(array+i);

}

printf("sum is %d",sum);
return sum;

}



 

Output