Summing the Elements of an Array using pointer - C Pointer

C examples for Pointer:Array Pointer

Description

Summing the Elements of an Array using pointer

Demo Code

#include <stdio.h>

int arraySum (int *array, const int n){
    int sum = 0;/*from  w  w w . j  a  va  2 s .  c o m*/
    int * const arrayEnd = array + n;

    for ( ; array < arrayEnd; ++array)
    {
        sum += *array;
    }

    return sum;
}

int main (void){
    int values[10] = {13, 7, -19, 13, 6, -11, 7, 91, 1, -5};

    printf ("The sum is %i\n", arraySum (values, 10));

    return 0;
}

Result


Related Tutorials