Skip to content

Commit c36b40a

Browse files
committed
use pointer to sum elements of array
1 parent 7185a8f commit c36b40a

File tree

1 file changed

+25
-0
lines changed

1 file changed

+25
-0
lines changed

prog.11.11.c

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
// Function to sum the elements of an integer array
2+
3+
#include <stdio.h>
4+
5+
int arraySum (int array[], const int n)
6+
{
7+
int sum = 0, *ptr;
8+
int * const arrayEnd = array + n;
9+
10+
for ( ptr = array; ptr < arrayEnd; ++ptr ) {
11+
sum += *ptr;
12+
}
13+
14+
return sum;
15+
}
16+
17+
int main (void)
18+
{
19+
int arraySum (int array[], const int n);
20+
int values[10] = { 3, 7, -9, 3, 6, -1, 7, 9, 1, -5 };
21+
22+
printf ("The sum is %i\n", arraySum (values, 10));
23+
24+
return 0;
25+
}

0 commit comments

Comments
 (0)