Thursday, November 6, 2014

How to change value of any declared and initialized integer by pointer?

Pointer is a powerful tool in C++. Pointer is a variable which contains address of variable. Let us declare 
a=30;
If we want to change the value of a then we usually type this code ;
a=40;
But using pointer we can change the value of a through its memory location without disturbing a this is the main thing that pointer enable us to make changes.
Here is an example.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
    int a=50; //declare and initialize a
    cout<<"The value of a before "<<a;
    int *b; //declare pointer
    b=&a;   //pass address of a to b
    *b=100; //change value of a by pointer
    cout<<"\nThe value of a after changing "<<a;
getch();
}

The output of above program is :

No comments:

Post a Comment