
- C Programming Tutorial
- C - Home
- C - Overview
- C - Environment Setup
- C - Program Structure
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Constants
- C - Storage Classes
- C - Operators
- C - Decision Making
- C - Loops
- C - Functions
- C - Scope Rules
- C - Arrays
- C - Pointers
- C - Strings
- C - Structures
- C - Unions
- C - Bit Fields
- C - Typedef
- C - Input & Output
- C - File I/O
- C - Preprocessors
- C - Header Files
- C - Type Casting
- C - Error Handling
- C - Recursion
- C - Variable Arguments
- C - Memory Management
- C - Command Line Arguments
- C Programming useful Resources
- C - Questions & Answers
- C - Quick Guide
- C - Useful Resources
- C - Discussion
How to calculate sum of array elements using pointers in C language?
Pointer is a variable which stores the address of other variable.
Consider the following statement −
int qty = 179;
Declaring a pointer
The syntax for declaring a pointer is as follows −
int *p;
Here, ‘p’ is a pointer variable which holds the address of other variable.
Initialization of a pointer
Address operator (&) is used to initialize a pointer variable.
For example,
int qty = 175; int *p; p= &qty;
Array of Pointers
It is collection of addresses (or) collection of pointers.
Declaration
Following is the declaration for array of pointers −
datatype *pointername [size];
For example,
int *p[5];
It represents an array of pointers that can hold five integer element addresses.
Initialization
‘&’ is used for initialization
For example,
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
Indirection operator (*) is used for accessing.
For example,
for (i=0, i<3; i++) printf ("%d", *p[i]);
Program
Following is the C program to calculate the sum of the array elements by using pointers −
//sum of array elements using pointers #include <stdio.h> #include <malloc.h> void 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; }
Output
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
- Related Articles
- C Program to find sum of perfect square elements in an array using pointers.
- Explain array of pointers in C programming language
- Go language Program to calculate the sum of matrix elements
- Demonstrate the concept of pointers using C language
- Explain pointers and one-dimensional array in C language
- Explain pointers and two-dimensional array in C language
- Explain Arithmetic operations using pointers in C language?
- Inserting elements in an array using C Language
- How to find the sum of elements of an Array using STL in C++?
- How to calculate the XOR of array elements using JavaScript?
- What are pointers to structures in C language?
- Explain the pointers to unions in C language
- Explain the concept of pointers in C language
- Swift Program to Calculate the sum of Elements in a Given Array
- C program to find sum and difference using pointers in function
