C++ provides two pointer operators, one is the address operator & and the other is the indirect address 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. Variables can be any data type, including objects, structures, or pointers.
Address operator &
& is a unary operator that returns the memory address of the operand. For example, if var is an integer variable, &var is its address. This operator has the same priority as other unary operators, and it is performed in order from right to left when it is operated.
You can read the & operator as the "address operator", which means that &var is read as the "address of var".
Indirect addressing operator *
The second operator is the indirect addressing operator, which is a supplement to the & operator. It is a unary operator that returns the value of the variable at the address specified by the operand.
Please see the example below 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 << "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
This is the end of this article about the implementation of C++ pointer operators (& and *). For more related contents of C++ pointer operators, please search for my previous articles or continue browsing the related articles below. I hope everyone will support me in the future!