Using Pointer Parameters to sum the elements of an array - C Pointer

C examples for Pointer:Pointer Variable

Description

Using Pointer Parameters to sum the elements of an array

Demo Code

#include <stdio.h>
#define SIZE 10/*from  w  ww .  j  a  v a  2 s.c o  m*/
int sump(int * start, int * end);
int main(void)
{
    int marbles[SIZE] = {202,102,52,392,42,162,192,226,321,220};
    long answer;
    
    answer = sump(marbles, marbles + SIZE);
    printf("The total number of marbles is %ld.\n", answer);
    
    return 0;
}

int sump(int * start, int * end){
    int total = 0;
    
    while (start < end){
        total += *start; // add value to total
        start++;         // advance pointer to next element
    }    
    return total;
}

Result


Related Tutorials