This is a C program to calculate sum of array elements using pointer.
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.
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
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
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));
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.
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.
/*
* C program to read N integers and store them in an array A.
* Find the sum of all these elements using pointer.
*/
#include <stdio.h>
#include <malloc.h>
void main()
{
int i, n, sum = 0;
int *a;
printf("Enter the size of array A \n");
scanf("%d", &n);
a = (int *) malloc(n * sizeof(int));
printf("Enter Elements of the List \n");
for (i = 0; i < n; i++)
{
scanf("%d", a + i);
}
/* Compute the sum of all elements in the given array */
for (i = 0; i < n; i++)
{
sum = sum + *(a + i);
/* this *(a+i) is used to access the value stored at the address*/
}
printf("Sum of all elements in array = %d\n", sum);
return 0;
}
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.
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
- Watch Advanced C Programming Videos
- Check C Books
- Practice BCA MCQs
- Apply for Computer Science Internship
- Apply for C Internship