Sum of Array Using Pointer Arithmetic in C++



The pointers in C++ are used to store memory addresses of variables. We can perform pointer arithmetic such as incrementing or decrementing pointers to traverse through an arrays. In this article, we will learn how to using pointer arithmetic in C++ to traverse through an array and find sum of it. First of all, let's understand the problem statement.

You are given an array of integers and you need to find the sum of all the elements in that array using pointer arithmetic. For example:

// Input array int arr[] = {1, 2, 3, 4, 5}; // Output Sum = 15 // Explanation: 1 + 2 + 3 + 4 + 5 = 15

To solve this problem, first we need to understand pointer arithmetic and how to use pointers to access array elements. Let's see how to perform pointer arithmetic in C++.

Pointer Arithmetic

The pointer arithmetic refers to the operations that can be performed on pointers to move through memory locations of variables. To understand this, let us consider that 'ptr' is an integer pointer which points to the address 1000. Now, if we perform an increment operation( ptr++) on 'ptr', it will point to the next integer address which is 1004 (if the size of an integer is 4 bytes). After moving to the next address, we can access the value stored at that address using the dereference operator (*). The code snippet below clearly shows this operation:

// Pointer Increment Operation int arr[] = {1, 2, 3, 4, 5}; int* ptr = arr; // Pointer to the first element ptr++; // Move to the next element cout << *ptr; // Output: 2, ptr points to the second element of the array // Pointer Decrement Operation ptr--; // Move back to the first element cout << *ptr; // Output: 1, ptr points back to the first element of the array

Sum of Array Using Pointer Arithmetic

Now, we understood how pointer arithmetic works. We have to use similar operations to traverse through the array and calculate the sum of it's elements. Following operations can be used to achieve this:

  • arr + i : This operation helps to move the pointer to the next element of the array.
  • *(arr + i) : This dereferences the pointer to get the value at that address.
  • sum += *(arr + i) : This adds the value at the current pointer address to the sum.

Example of Sum of Array Using Pointer Arithmetic

In the code below, we have defined a function sumOfArray that takes an integer pointer and the size of the array as arguments to perform the above discussed operations:

Open Compiler
#include <iostream> using namespace std; int sumOfArray(int* arr, int size) { int sum = 0; for (int i = 0; i < size; ++i) { sum += *(arr + i); // Pointer arithmetic to access array elements } return sum; } int main() { // Input array int arr[] = {1, 2, 3, 4, 5}; int size = sizeof(arr) / sizeof(arr[0]); // Calculate size of the array int sum = sumOfArray(arr, size); // Call function to calculate sum cout << "Sum of array elements: " << sum << endl; // Output the result return 0; }

The output of the above code will be:

Sum of array elements: 15
Updated on: 2025-05-27T18:08:05+05:30

3K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements