C Program to Find the Sum of Array Elements using Pointers

In this article, we will write a C program to find the sum of array elements using pointers.

The program takes the size of the array and the array elements from the user, calculates the sum of the array elements, and prints it on the screen as output.

Sample Input:

Enter the size of the array: 5
Enter array elements: 1 2 3 4 5

Output:

The sum of array elements is: 15

In C programming language, a pointer is a special type of variable that is used to store the memory address of another variable. It is declared by putting an asterisk symbol (*) before the variable name, for eg. *myPtr.

To calculate the sum of array elements using pointers, you have to first assign the address of the first element of the array to the pointer and access its value by dereferencing(putting * before it) the pointer.

Now, to get the second element of the array, you have to increment the pointer by 1 and dereference it to get its value. This process will keep on repeating until we get the last element of the array.

The following C program shows how you can get the sum of array elements using pointers:

// C program to calculate the sum of array elements using pointers
#include <stdio.h>

int main() {

    int arr[100], size;
    int *ptr, sum = 0;

    printf("Enter the size of the array: ");
    scanf("%d", &size);

    printf("Enter array elements: ");
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    // Set address of first array element to *ptr
    ptr = arr;

    for (int i = 0; i < size; i++) {
        sum = sum + *ptr;
        ptr++; // Increment pointer by one to get next element
    }

    printf("The sum of array elements is: %d", sum);

    return 0;

}

Output:

Enter the size of the array: 5
Enter array elements: 1 2 3 4 5
The sum of array elements is: 15

Example 2: Using Array Index with Pointers to Get the Sum

In the last example, we incremented the pointer *ptr by one in each iteration of the for loop to get the next element of the array.

But, instead of incrementing the pointer, we can also use the array index with the pointer to access each element of the array.

For example, to access the element at ith index, we can use the following syntax:

// Gives the element at ith index of array
int elem = *(ptr + i);

The following C program shows how you can use the pointer with the array index to get the sum:

// C program to calculate the sum of array elements using pointers
#include <stdio.h>

int main() {

    int arr[100], size;
    int *ptr, sum = 0;

    printf("Enter the size of the array: ");
    scanf("%d", &size);

    printf("Enter array elements: ");
    for (int i = 0; i < size; i++) {
        scanf("%d", &arr[i]);
    }

    // Set address of first array element to *ptr
    ptr = arr;

    for (int i = 0; i < size; i++) {
        sum = sum + *(ptr + i);

        // *(ptr + i) gives element at ith index of array
    }

    printf("The sum of array elements is: %d", sum);

    return 0;

}

Output:

Enter the size of the array: 6
Enter array elements: 1 2 3 4 5 6
The sum of array elements is: 21

Thanks for reading.

Author

  • Manoj Kumar

    Hi, My name is Manoj Kumar. I am a full-stack developer with a passion for creating robust and efficient web applications. I have hands-on experience with a diverse set of technologies, including but not limited to HTML, CSS, JavaScript, TypeScript, Angular, Node.js, Express, React, and MongoDB.

    View all posts