Easy Tutorial
❮ C Function Strftime Cpp Nested If ❯

C++ Pointer Operators (& and *)

C++ Operators

C++ provides two pointer operators, one is the address-of operator &, and the other is the dereference operator *.

A pointer is a variable that contains the address of another variable. You can say that a variable containing the address of another variable is "pointing to" that variable. The variable can be of any data type, including objects, structures, or pointers.

Address-of Operator &

& is a unary operator that returns the memory address of its operand. For example, if var is an integer variable, then &var is its address. This operator has the same precedence as other unary operators and is evaluated from right to left.

You can read the & operator as "address-of operator", which means &var is read as "address of var".

Dereference Operator *

The second operator is the dereference operator *, which is the complement of the & operator. * is a unary operator that returns the value of the variable located at the address specified by its operand.

Consider the following example to understand the usage of these operators.

Example

#include <iostream>

using namespace std;

int main ()
{
   int  var;
   int  *ptr;
   int  val;

   var = 3000;

   // Get the address of var
   ptr = &var;

   // Get the value at ptr
   val = *ptr;
   cout << "Value of var :" << var << endl;
   cout << "Value of ptr :" << ptr << endl;
   cout << "Value of val :" << val << endl;

   return 0;
}

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

C++ Operators

❮ C Function Strftime Cpp Nested If ❯