Skip to content
Tutorials Center
My learning

Topics

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.

C++
int x = 5;
int* p = &x;   // address-of
*p = 6;        // dereference: x is now 6

Try it

C++ Taking an address and following 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.

C++ Dereferencing iterators, smart pointers and optional

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

C++
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 &5 or &f() (when f returns 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.member is an ordinary pointer.
  • Address of a function: &f and plain f both give a function pointer, and *fp yields the function again, so (*fp)(1) and fp(1) are the same call.
  • Arrays: &arr is a pointer to the whole array (type int(*)[3]), while arr decays to int*. They hold the same address but step differently in pointer arithmetic.
  • Overloaded &: a class can overload unary operator&, which breaks generic code. Use std::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: *p gives the element and p->m is shorthand for (*p).m. See member access operators.
  • Reading through a null std::optional with * is undefined behavior; value() throws std::bad_optional_access instead.
  • Dereferencing is an lvalue operation: *p = 3 assigns, and &*p does not evaluate any read of the object.
  • A reference is not a pointer. int& r = x; declares an alias; &r is the address of x, not of the reference.
  • Don't print raw addresses in test output: they differ between runs. Compare pointers with == instead.