C Program to Find Sum of Array Elements using Pointer as Arguments

This C Program calculates the sum of all elements of an array using pointers as arguments.

Problem Description

The program calls a function to add all the element of an array and passes the array argument as a pointer.

Problem Solution

1. Create a static array of some fixed size, along with element declaration.
2. Build a function with a single argument, in which we will pass array argument.
3. Inside this function, all the elements of the array are accessed one-by-one, adding to return the sum.
4. Return sum to the called function and print it.

Program/Source Code

Here is source code of the C program to calculate sum of all elements of an array using pointers as arguments. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

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

1. Declare a static array of some fixed size along with array element definition.
2. Take a variable sum, which will store the total sum of all the array elements.
3. Create a function with a single argument, in which we will pass the above created array argument. This function will return the sum.
4. Inside this function, a for loop will run from 0 to array size-1, accessing each element of array by adding the iterator i to the pointer variable (array argument, which was passed to this function).
5. Elements will get add and the total sum will be returned from this function.
6. Variable sum inside main() function, will get this returned value and will be printed.

advertisement
Runtime Test Cases
 
Sum of all array elements =  3000

Sanfoundry Global Education & Learning Series – 1000 C Programs.

Here’s the list of Best Books in C Programming, Data Structures and Algorithms.

🎓 Free Certifications on 300 subjects are live for December 2025. Register Now!

advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification to get free Certificate of Merit. Join our social networks below and stay updated with latest contests, videos, internships and jobs!

Youtube | Telegram | LinkedIn | Instagram | Facebook | Twitter | Pinterest
Manish Bhojasia - Founder & CTO at Sanfoundry
I’m Manish - Founder and CTO at Sanfoundry. I’ve been working in tech for over 25 years, with deep focus on Linux kernel, SAN technologies, Advanced C, Full Stack and Scalable website designs.

You can connect with me on LinkedIn, watch my Youtube Masterclasses, or join my Telegram tech discussions.

If you’re in your 20s–40s and exploring new directions in your career, I also offer mentoring. Learn more here.