What is pointer operator & in C++?


C++ provides two pointer operators, which are Address of Operator (&) and Indirection Operator (*). A pointer is a variable that contains the address of another variable or you can say that a variable that contains the address of another variable is said to "point to" the other variable. A variable can be any data type including an object, structure or again pointer itself.

The address of Operator (&), and it is the complement of *. It is a unary operator that returns the address of the variable(r-value) specified by its operand. For example,

Example

#include <iostream>
using namespace std;
int main () {
   int  var;
   int  *ptr;
   int  val;
   var = 3000;
   ptr = &var;     // take the address of var
   val = *ptr;     // take the value available at ptr
   cout << "Value of var :" << var << endl;
   cout << "Value of ptr :" << ptr << endl;
   cout << "Value of val :" << val << endl;
   return 0;
}

Output

When the above code is compiled and executed, it produces the following result −

Value of var : 3000
Value of ptr : 0xbff64494
Value of val : 3000

Updated on: 10-Feb-2020

2K+ Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements