explain c++ pointers code example

Example 1: pionter in c++

#include <iostream>
using namespace std;
int main(){
   //Pointer declaration
   int *p, var=101;
 
   //Assignment
   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: pointer in c++

int* pointVar, var;
var = 5;

// assign address of var to pointVar
pointVar = &var;

// access value pointed by pointVar
cout << *pointVar << endl;   // Output: 5

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.

Tags:

Cpp Example