C Program to Calculate Sum of all Elements of an Array using Pointers as Arguments


  1. /*
  2.  * C program to find the sum of all elements of an array using 
  3.  * pointers as arguments.
  4.  */
  5. #include <stdio.h>
  6. 
    
  7. void main()
  8. {
  9.     static int array[5] = { 200, 400, 600, 800, 1000 };
  10.     int sum;
  11. 
    
  12.     int addnum(int *ptr);
  13. 
    
  14.     sum = addnum(array);
  15.     printf("Sum of all array elements = %5d\n", sum);
  16. }
  17. int addnum(int *ptr)
  18. {
  19.     int index, total = 0;
  20.     for (index = 0; index < 5; index++)
  21.     {
  22.         total += *(ptr + index);
  23.     }
  24.     return(total);
  25. }

 

output:

Sum of all array elements =  3000