Search This Blog

Wednesday, 19 February 2014

simple program to pass pointer to a function

//  pointer to function

/*
  pointer is a variable which stores the address of the another variable.
  ex - int a = 3;
         int *ptr = &a;
         here *ptr is a pointer variable whose value is 2 and contains address of the value 2. 
*/


#include<iostream.h>
#include<conio.h>

void add(int *a)
{
  int i,sum=0;
  for(i=1;i<=*a;i++)
     sum+=i;
     *a=sum;
}

void main()
{
    int n;
    cout<<"Enter the limit :";
    cin>>n;
    int temp=n;
    add(&n);
    cout<<"Sum from 0 to "<<temp<<" is : "<<n;

    getch();
}

Output

Enter the limit :10
Sum from 0 to 10 is : 55

No comments:

Popular Posts