Sunday, October 2, 2022
HomeC Programmingprogram for pointer in c

program for pointer in c


                                 

 

Code:

#embrace <stdio.h>

int foremost() {

    int a[100];

    int i,n,sum=0;

    int *p;

    printf(“Enter a Quantity:”);

    scanf(%d,&n);

    printf(“Enter %d parts:”,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 parts=%d,sum);

    return 0;

}

Pattern Enter and Output:

Enter a Quantity:4

Enter 4 parts:2

1

5

6

Sum of parts=14

On this article we’re going to see easy methods to write program to sum of all parts saved in an array utilizing pointer

What’s Pointer ?

In C Programming Language Pointer is a variable which is used to retailer the handle of one other variable.The variable might be of any kind like int,float,char and so forth.It’s retailer to retailer and handle the handle of dynamically allotted blocks of reminiscence.

Clarification of above program 

Within the first line of code,we have now included stdio.h header file which can assist us to make use of among the built-in operate like printf() and scanf() that can be used to print the assertion and take console enter respectively.

Within the subsequent line of code,we have now outlined foremost operate which can begin the execution of our C program,we’ll outline the core of our program in foremost operate solely and never use user-defined operate for a similar.

Within the subsequent line of code,we have now declared one array with measurement 100 and kind int,we’ll use this array to take all of the factor from consumer of which we have now to fined  sum utilizing pointers.

Within the subsequent line we have now declared three variable i,n and sum=0 the place i can be used as iteration variable i.e in for loop , n to take complete no.of factor of an array and sum to retailer the results of addition of all parts

Within the subsequent line, we have now declared one pointer of identify p,we’ll use this to retailer the beginning handle of first factor of a array and remaining factor of array can be fetched iteratively.

After that we have now printed one assertion ‘Enter a Quantity’ assertion which can assist consumer to get to identified that we have now to present some enter then solely this system will proceed since we have now wrote one scanf() assertion which can truly pause the execution till enter is supplied.

Within the subsequent line we have now wrote yet another print assertion will assist consumer to get to identified that how a lot factor we have now present to make an array,we’re reaching it utilizing following assertion printf(“Enter %d parts:”,n); right here the worth of n can be concatenated with Enter factor assertion for instance if consumer entered the worth of n=3 then this print assertion can be evaluated as under Enter 3 factor,In above assertion we have now %d format specifier as a result of n is of int(Integer) kind.

Within the subsequent line of ,we have now outlined one for loop which can assist us to take parts of array one after the other and retailer it in an a array by indexing methodology.For those who did not get it let’s perceive by iteration by iteration.

for(i=0;i<n;i++)

The pattern of n = 3

1st Iteration

i = 0(Assigned i=0 for the reason that array indexing begins from 0)

0<3 //Since(i<n) Due to this fact the management will go contained in the for loop and execute all of the assertion inside it.

Within the first line of for loop we’re utilizing scanf( “%d”,&a[i]) the that means of this assertion is taking the consumer enter of  kind int and retailer it in a[0] for the reason that worth of i on this iteration is 0 subsequently it can retailer the enter worth in a[0].

the worth of i can be incremented

2nd iteration 

i =1 (Because the worth of i is incremented by 1 because it was beforehand 0)

1<3 //Since(i<n) Situation glad subsequently the management will go contained in the for loop and execute the statements

It is going to once more take consumer enter as a second factor of an array and can retailer it in a[1] as the worth of i is 1 

 

For those who any have doubt,suggestion relating to this submit or web site then be happy to share with us.

 

In case you have discovered one thing new from this submit then share this submit with your loved ones and
pals.

 

Glad Studying :)😎   

 

Earlier
Subsequent

RELATED ARTICLES

LEAVE A REPLY

Please enter your comment!
Please enter your name here

Most Popular

Recent Comments