YouTip LogoYouTip

Cpp Pointer Operators

# C++ Pointer Operators (& and *) [![Image 3: C++ Operators](#) C++ Operators](#) C++ provides two pointer operators: the address-of operator `&` and the dereference operator `*`. A pointer is a variable that contains the memory address of another variable. You can say that a variable containing the memory 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 it is evaluated from right to left. You can read the `&` operator as **"address-of" operator**, meaning that **`&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. Please see the following example 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 above code is compiled and executed, it produces the following result: Value of var :3000Value of ptr :0xbff64494Value of val :3000 [![Image 4: C++ Operators](#) C++ Operators](#)
← Docker Load CommandJquery Cookie Plugin β†’