C++ Reference ยท Operators
The address-of (&) and dereference (*) operators
Reference for the C++ unary & and * operators: taking addresses, dereferencing pointers, iterators and smart pointers, std::addressof and overloading.
The unary & operator yields the address of an object: &x is a pointer to x. The unary * operator does the reverse: it follows a pointer (or any pointer-like object) and yields the object it refers to, as an lvalue you can read or assign to. They are inverses: *&x is x, and &*p is p for a valid pointer.
Neither is the same as the binary a & b (bitwise AND) or a * b (multiplication), and & and * in declarations such as int& r or int* p are part of the type, not operators. Class types can overload both operators: * is overloaded by iterators, std::unique_ptr, std::shared_ptr and std::optional.
int x = 5; int* p = &x; // address-of *p = 6; // dereference: x is now 6
Try it
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Press Run (or Ctrl + Enter) to see the result here.
Program input ยท what you'd type while the program runs
Type the answers before you press Run, one per line. Each time the program reads a line (with std::cin or std::getline), it gets the next line from this box.
Syntax
T* operator&(T& lvalue); // built-in address-of: &expr T& operator*(T* pointer); // built-in dereference: *expr T* std::addressof(T& object) noexcept; // <memory>
| Name | Description |
|---|---|
&expr |
expr must be an lvalue (or a qualified name of a member, which gives a pointer to member). The result is a prvalue pointer. |
*expr |
expr must be a pointer to an object or a function. The result is an lvalue of the pointed-to type. |
Returns: a pointer for &; an lvalue (a reference to the object) for *.
Errors / undefined behavior:
- Dereferencing a null, dangling, uninitialized or past-the-end pointer is undefined behavior.
- Dereferencing a
void*is ill-formed; cast it to a typed pointer first. &cannot be applied to an rvalue such as&5or&f()(whenfreturns by value), to a bit-field, or to a register-like temporary.
Usage notes
- Address of a member:
&Class::member(with the class name) is a pointer to member, not a plain pointer;&obj.memberis an ordinary pointer. - Address of a function:
&fand plainfboth give a function pointer, and*fpyields the function again, so(*fp)(1)andfp(1)are the same call. - Arrays:
&arris a pointer to the whole array (typeint(*)[3]), whilearrdecays toint*. They hold the same address but step differently in pointer arithmetic. - Overloaded
&: a class can overload unaryoperator&, which breaks generic code. Usestd::addressof(x)when you need the true address (the standard library does). Overloading&is almost never a good idea. - Smart pointers and iterators model the same idea:
*pgives the element andp->mis shorthand for(*p).m. See member access operators. - Reading through a null
std::optionalwith*is undefined behavior;value()throwsstd::bad_optional_accessinstead. - Dereferencing is an lvalue operation:
*p = 3assigns, and&*pdoes not evaluate any read of the object. - A reference is not a pointer.
int& r = x;declares an alias;&ris the address ofx, not of the reference. - Don't print raw addresses in test output: they differ between runs. Compare pointers with
==instead.