Calculate Sum of Array Elements Using Pointers in C Language



Pointers

A pointer is a variable that stores the address of another variable. Instead of holding a value directly, it holds the address where the value is stored in memory. The two main operators used with pointers are the dereferencing operator and the address operator. These operators are used to declare pointer variables and access the values stored at those addresses.

The pointer variable uses the dereferencing operator to access the value it points to. We use the address operator to obtain the memory address of a variable and store it in the pointer variable.

We use pointers to access the memory of the variable, and then this manipulates their address in a program. The pointers are very different features that provide the language with flexibility and power.

Consider the following statement ?

int qty = 179;

The syntax for declaring a pointer is as follows ?

int *p;

Here, ?p' is a pointer variable which holds the address of other variable.

Following are the steps to initialize a pointer ?

  • Initialization of a pointer

  • Array of Pointers

  • Initialization of Array Pointers

  • Accessing a Pointer

Initialization of a Pointer

Address operator (&) initializes a pointer variable. This code declares an integer variable with a value of 175, then declares a pointer p and assigns it the address of qty. Now, p points to qty.

int qty = 175;
int *p;
p= &qty;

Array of Pointers

It is collection of addresses (or) collection of pointers that are references to a memory location. This is generally determined in C programming where we need to point at multiple memory locations of a similar data type. We need access to the data by dereferencing the pointer pointing to it.

Here is the declaration for an array of pointers, where datatype specifies the type, pointer name is the array name, and [size] defines its length.

datatype *pointername [size];

This code declares an array of five integer pointers, where each element in the array can point to an integer.

It represents an array of pointers that can hold five integer element addresses.

int *p[5];

Initialization of Array Pointers

This code declares an integer array a with values 10, 20 and 30. It also declares an array of integer pointers p. The loop assigns each pointer in p to the corresponding element in a.

int a[3] = {10,20,30};
int *p[3], i;
for (i=0; i<3; i++) (or) for (i=0; i<3,i++)
p[i] = &a[i];
p[i] = a+i;

Accessing a Pointer

This code determines through the array p and prints the values pointed to each pointer in the array p.

for (i=0, i<3; i++)
printf ("%d", *p[i]);

Example

Here is a C program that calculates the sum of array elements using pointers. This code dynamically allocates memory for the array, reads the elements, sums them up, and prints the total sum.

// sum of array elements using pointers
#include <malloc.h>
#include <stdio.h>

int main() {
   int i, n, sum = 0;
   int *ptr;
   printf("Enter size of array : 
"); scanf("%d", &n); ptr = (int *)malloc(n * sizeof(int)); printf("Enter elements in the List
"); for (i = 0; i < n; i++) { scanf("%d", ptr + i); } // calculate sum of elements for (i = 0; i < n; i++) { sum = sum + *(ptr + i); } printf("Sum of all elements in an array is = %d
", sum); return 0; }

When the above program is executed, it produces the following result ?

Enter size of array:
5
Enter elements in the List
12
13
14
15
16
Sum of all elements in an array is = 70
Updated on: 2025-01-20T17:20:43+05:30

19K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements