C Program to Find Sum of Array Elements using Pointer

This is a C program to calculate sum of array elements using pointer.

Problem Description

We have to write a program in C such that it calculates the sum of elements of an array using pointers. The program should dynamically allocate a piece of memory for that array and use a pointer to point to that array memory as well as traverse that array using a pointer.

Expected Input and Output

If we are entering 5 elements (N = 5), with array element values as 4, 9, 10, 56 and 100 then,
Sum of Elements of the array will be: 4 + 9 + 10 + 56 + 100 = 179

Problem Solution

Pointers are used to store Address of variables or a memory location. In C, a pointer is created by placing a * before the variable name. We can access the value of the pointer by using * before the name of the pointer variable.

For example, int *x; /* this creates a pointer with name x */

This program uses malloc() function of C. This function is used for memory allocation. The syntax of malloc is

advertisement
advertisement

pointer_name = (cast-type*) malloc (byte-size)

For example, if you have to allocate 20 bytes of storage to an integer pointer named ptr, then we will use the following C code:

int *ptr = (int*) malloc (5 * sizeof(int));

Sanfoundry Certification Contest of the Month is Live. 100+ Subjects. Participate Now!

The sequence of steps for the solution will be as follows:
1. Create a pointer variable, which points to an integer data.
2. Take a number i.e size of array as input.
3. Create a block of space fo size (size-of-array*sizeof(int)) using malloc() assigning the starting address of this whole space to the pointer variable.
4. Iterate via for loop for reading array elements as input.
5. Iterate again via for loop to access the address stored in pointer variable, adding the iterator value to it, so as to access every element of array, and calculating the overall sum.

Program/Source Code

Here is source code of the C program to calculates the sum of array elements using pointer. The program is successfully compiled and tested using Turbo C compiler in windows environment. The program output is also shown below.

  1. /*
  2.  * C program to read N integers and store them in an array A.
  3.  * Find the sum of all these elements using pointer.
  4.  */
  5.  
  6. #include <stdio.h>
  7. #include <malloc.h>
  8.  
  9. void main()
  10. {
  11. 	int i, n, sum = 0;
  12. 	int *a;
  13.  
  14.      	printf("Enter the size of array A \n");
  15. 	scanf("%d", &n);
  16.  
  17.         a = (int *) malloc(n * sizeof(int));
  18.  
  19.         printf("Enter Elements of the List \n");
  20. 	for (i = 0; i < n; i++) 
  21.         {
  22. 		scanf("%d", a + i);
  23. 	}
  24.  
  25.         /*  Compute the sum of all elements in the given array */
  26.  
  27.         for (i = 0; i < n; i++)
  28.         {
  29. 		sum = sum + *(a + i); 
  30.                /* this *(a+i) is used to access the value stored at the address*/
  31. 	}
  32.  
  33.         printf("Sum of all elements in array = %d\n", sum);
  34.         return 0;
  35. }
Program Explanation

1. Create a pointer variable a, which points to an integer data.
2. Now, create another variable n (size of array), whose input should be taken by users, and a variable sum (initial value of sum = 0), which will store the total sum of all the elements of the array.
3. Now allocate size_of_array(N) * size_of_each_element (integer) times space using malloc() function, assigning the starting address of this space to the pointer variable, a.
4. Using for loop (0 to size), start iteration, reach at every array element location by adding the value of i to the value of a, taking array element as input.
5. Again, start iteration, this time reach each array element location, and accessing them to add to the sum variable.
6. Print sum.

advertisement
Runtime Test Cases

Here is the runtime output of the C program where the user is reading an array of 5 integers with values 4, 9, 10, 56 and 100 and the program is calculating the sum of the elements of the array using pointer and then displaying the result.

Enter the size of array A
5
Enter Elements of the List
4
9
10
56
100
Sum of all elements in array = 179

Sanfoundry Global Education & Learning Series – 1000 C Programs.

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

advertisement
If you wish to look at programming examples on all topics, go to C Programming Examples.

advertisement
advertisement
Subscribe to our Newsletters (Subject-wise). Participate in the Sanfoundry Certification contest 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
Manish Bhojasia, a technology veteran with 20+ years @ Cisco & Wipro, is Founder and CTO at Sanfoundry. He lives in Bangalore, and focuses on development of Linux Kernel, SAN Technologies, Advanced C, Data Structures & Alogrithms. Stay connected with him at LinkedIn.

Subscribe to his free Masterclasses at Youtube & discussions at Telegram SanfoundryClasses.