Code:
#include <stdio.h>
int main() {
int a[100];
int i,n,sum=0;
int *p;
printf("Enter a Number:");
scanf("%d",&n);
printf("Enter %d elements:",n);
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
p=a;
for(i=0;i<n;i++)
{
sum=sum+*p;
p++;
}
printf("Sum of elements=%d",sum);
return 0;
}
Sample Input and Output:
Enter a Number:4
Enter 4 elements:2
1
5
6
Sum of elements=14
In this article we are going to see how to write program to sum of all elements stored in an array using pointer
What is Pointer ?
In C Programming Language Pointer is a variable which is used to store the address of another variable.The variable can be of any type like int,float,char etc.It is store to store and manage the address of dynamically allocated blocks of memory.
Explanation of above program
In the first line of code,we have included stdio.h header file which will help us to use some of the built-in function like printf() and scanf() that will be used to print the statement and take console input respectively.
In the next line of code,we have defined main function which will start the execution of our C program,we will define the core of our program in main function only and not use user-defined function for the same.
In the next line of code,we have declared one array with size 100 and type int,we will use this array to take all the element from user of which we have to fined sum using pointers.
In the next line we have declared three variable i,n and sum=0 where i will be used as iteration variable i.e in for loop , n to take total no.of element of an array and sum to store the result of addition of all elements
In the next line, we have declared one pointer of name p,we will use this to store the starting address of first element of a array and remaining element of array will be fetched iteratively.
After that we have printed one statement 'Enter a Number' statement which will help user to get to known that we have to give some input then only the program will proceed since we have wrote one scanf() statement which will actually pause the execution until input is provided.
In the next line we have wrote one more print statement will help user to get to known that how much element we have provide to make an array,we are achieving it using following statement printf("Enter %d elements:",n); here the value of n will be concatenated with Enter element statement for example if user entered the value of n=3 then this print statement will be evaluated as below Enter 3 element,In above statement we have %d format specifier because n is of int(Integer) type.
In the next line of ,we have defined one for loop which will help us to take elements of array one by one and store it in an a array by indexing method.If you didn't get it let's understand by iteration by iteration.
for(i=0;i<n;i++)
The sample of n = 3
1st Iteration
i = 0(Assigned i=0 since the array indexing starts from 0)
0<3 //Since(i<n) Therefore the control will go inside the for loop and execute all the statement inside it.
In the first line of for loop we are using scanf( "%d",&a[i]) the meaning of this statement is taking the user input of type int and store it in a[0] since the value of i in this iteration is 0 therefore it will store the input value in a[0].
the value of i will be incremented
2nd iteration
i =1 (Since the value of i is incremented by 1 as it was previously 0)
1<3 //Since(i<n) Condition satisfied therefore the control will go inside the for loop and execute the statements
It will again take user input as a second element of an array and will store it in a[1] as the value of i is 1
If you any have doubt,suggestion regarding this post or website then feel free to share with us.
If you have learned something new from this post then share this post with your family and
friends.
friends.
Happy Learning :)😎
Tags:
basic pointer program in c
function pointer program in c
pointer example in c programming
pointer program in c example
pointer to function program in c
simple pointer program in c