Basic use of pointer in C++ Programming Language. The following code shows how to use pointer in C plus plus Programming Language.
Code:
#include<iostream>
using namespace std;
int main()
{
int a = 5, b = 8;
int *p;
p = &a;
cout << "Location of p is: " << p <<endl;
cout << "Value of p is: " << *p <<endl;
// Change Location
p = &b;
cout << "Changed Location of p is: " << p <<endl;
cout << "Changed Value of p is: " << *p <<endl;
// Change value of pointer
*p = 50;
cout << "Changed Location of p is: " << p <<endl;
cout << "Changed Value of p is: " << *p <<endl;
// Change value of Variable
b = 100;
cout << "Changed Location of p is: " << p <<endl;
cout << "Changed Value of p is: " << *p <<endl;
return 0;
}
Output:
Location of p is: 0x7ffee81f7a68
Value of p is: 5
Changed Location of p is: 0x7ffee81f7a64
Changed Value of p is: 8
Changed Location of p is: 0x7ffee81f7a64
Changed Value of p is: 50
Changed Location of p is: 0x7ffee81f7a64
Changed Value of p is: 100