Write a ‘C’ Program to compute the sum of all elements stored in an array using pointers
C Program to compute sum of the array elements using pointers
Program :
#include<stdio.h> #include<conio.h> void main() { int a[10]; int i,sum=0; int *ptr; printf("Enter 10 elements:\n"); for(i=0;i<10;i++) scanf("%d",&a[i]); ptr = a; /* a=&a[0] */ for(i=0;i<10;i++) { sum = sum + *ptr; //*p=content pointed by 'ptr' ptr++; } printf("The sum of array elements is %d",sum); }
Output :
Enter 10 elements : 11 12 13 14 15 16 17 18 19 20 The sum of array elements is 155