Indirection Operator: *
Grammar
- unary-expression:
- * cast-expression
The unary indirection operator (*) dereferences a pointer; that is, it converts a pointer value to an l-value. The operand of the indirection operator must be a pointer to a type. The result of the indirection expression is the type from which the pointer type is derived. The use of the * operator in this context is different from its meaning as a binary operator, which is multiplication.
If the operand points to a function, the result is a function designator. If it points to a storage location, the result is an l-value designating the storage location.
The indirection operator may be used cumulatively to dereference pointers to pointers. For example:
// expre_Indirection_Operator.cpp
// compile with: /EHsc
// Demonstrate indirection operator
#include <iostream>
using namespace std;
int main() {
int n = 5;
int *pn = &n;
int **ppn = &pn;
cout << "Value of n:\n"
<< "direct value: " << n << endl
<< "indirect value: " << *pn << endl
<< "doubly indirect value: " << **ppn << endl
<< "address of n: " << pn << endl
<< "address of n via indirection: " << *ppn << endl;
}
If the pointer value is invalid, the result is undefined. The following list includes some of the most common conditions that invalidate a pointer value.
- The pointer is a null pointer.
- The pointer specifies the address of a local item that is not visible at the time of the reference.
- The pointer specifies an address that is inappropriately aligned for the type of the object pointed to.
- The pointer specifies an address not used by the executing program.
See Also
Expressions with Unary Operators | C++ Operators | Operator Precedence and Associativity | Address-of Operator: & | Indirection and Address-of Operators (C Reference)