C++ Pointer Operators (& and *)
C++ provides two pointer operators: the address-of operator & and the indirection operator *.
A pointer is a variable that contains the address of another variable; you can say a variable that contains the address of another variable "points to" that other 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 "the address of var".
Indirection Operator *
The second operator is the indirection operator , which is the complement of the & operator. is a unary operator that returns the value of the variable at the address specified by the operand.
See the example below to understand the usage of these two operators.
Example
#include
using
namespace
std
;
int
main
(
)
{
int
var
;
int
ptr
;
int
val
;
var
=
3000
;
// Get the address of var
ptr
=
&
var
;
// Get the value of 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 code above is compiled and executed, it produces the following result:
Value of var :3000
Value of ptr :0xbff64494
Value of val :3000
C++ Operators