Welcome

Hi! This blog is for beginners of c++ and java .You can check and pick codes for your programming assignments. This will also help you to understand the concepts of these programming languages. Its all about programming.

Using Of Pointers in Functions

Friday, July 1, 2011

/*
  Write a program using pointers to input any number
  and then pass that number to the function which will
  increment the value pointed by that pointer by 1
*/
#include<iostream.h>

void increment(int* p);        // function declaration
int main(){
   int a;
  
   int* p=&a;    // declaring pointer p and intializing it to address of integer a
  
   cout << " Enter a number: ";
   cin >> a;
  
   cout << endl << "You have enetered: " << a << endl;
  
   increment(p);
  
   cout << endl << "Value after incrementing is: " << a << endl;
  
   // Another way to call for function is to send the address like below
  
   increment(&a);
  
   cout << endl << "Value after second increment is: " << a << endl;
         
return 0;
} // end main()

// ========= increment() ================
void increment(int* p1){
   (*p1)++;
return;
} // end increment()