Latest web development tutorials

C ++ pointer operators (& and *)

C ++ operator C ++ operator

C ++ provides two pointer operators, one is the address operator & A is the indirection operator *.

A pointer is a variable that contains the address of another variable, you can put a variable that contains the address of another variable is said to "point" to another variable. Variables can be any type of data, including the object, or pointer to the structure.

The address operator &

& Is a unary operator returns the memory address of the operand. For example, if var is an integer variable, then & var is its address. The operator with the other unary operators have the same priority, it is at the time of operation of the order from right to left.

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

Indirection operator *

The second operator is the indirection operator *, which complements the & operator. * Is a unary operator, the return value of the variable operand address specified.

Consider the following examples to understand the usage of these two operators.

#include <iostream>
using namespace std;
int main ()
   int  var;
   int  *ptr;
   int  val;

   var = 3000;

   // 获取 var 的地址
   ptr = &var;

   // 获取 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 results:

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

C ++ operator C ++ operator