English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
C++ It provides two pointer operators, one is the address-of operator & and one is the indirect addressing operator *.
A pointer is a variable that contains the address of another variable. You can say that a variable that contains the address of another variable is "pointing to" another variable. The variable can be of any data type, including objects, structures, or pointers.
& is a unary operator that returns the memory address of the 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 executed from right to left in the order of operation.
You can read the & operator as"address-of operator"This means that,&var Read as "the address of var".
The second operator is the indirect addressing operator *It is a supplement to the & operator.* It is a unary operator that returns the value of the variable specified by the operand.
Please see the following example to understand the usage of these two operators.
#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 of ptr val = *ptr; cout << "The value of var: " << var << endl; cout << "The value of ptr: " << ptr << endl; cout << "The value of val: " << val << endl; return 0; }
When the above code is compiled and executed, it will produce the following results:
The value of var:3000 The value of ptr: 0xbff64494 The value of val:3000