how pointers work c++ code example
Example 1: pionter in c++
#include <iostream>
using namespace std;
int main(){
int *p, var=101;
p = &var;
cout<<"Address of var: "<<&var<<endl;
cout<<"Address of var: "<<p<<endl;
cout<<"Address of p: "<<&p<<endl;
cout<<"Value of var: "<<*p;
return 0;
}
Example 2: c++ pointers
#include <iostream>
using namespace std;
int main () {
int var = 20;
int *ip;
ip = &var;
cout << "Value of var variable: ";
cout << var << endl;
cout << "Address stored in ip variable: ";
cout << ip << endl;
cout << "Value of *ip variable: ";
cout << *ip << endl;
return 0;
}
Example 3: pointer in c++
int a = 5;
cout << a;
int a = 5;
int *ab;
ab = &a;
cout << ab;
Example 4: pointer in c++
int* pointVar, var;
var = 5;
pointVar = &var;
cout << *pointVar << endl;
In the above code, the address of var is assigned to the pointVar pointer.
We have used the *pointVar to get the value stored in that address.