sum of array elements using pointers

Solutions on MaxInterview for sum of array elements using pointers by the best coders in the world

showing results for - "sum of array elements using pointers"
Nicolás
03 Nov 2016
1#include <stdio.h>
2
3int main()
4{
5   int n,i;
6   printf("How many elements do you want in you array:\n");
7   scanf("%d",&n);
8   int ara[n];
9   for(i=0;i<n;i++){
10       scanf("%d",&ara[i]);
11   }
12   printf("\n");
13   printf("The created array is:\n");
14   
15   for(i=0;i<n;i++){
16       printf("%d ",ara[i]);
17   }
18   printf("\n\n");
19   //
20   int *ptr,sum=0;
21   for(ptr=&ara[0];ptr<=&ara[n];ptr++){
22       sum+=*ptr;
23   }
24   
25   printf("Sum is %d\n",sum);
26   
27
28    return 0;
29}
30