Knowledge baseExam Center
ENEnglish
Choose language13

CKTIS / C++

Dereference operator *

The unary * operator denotes the object or function addressed by a pointer. It enables reading and, when the type permits, modifying the pointed-to object; it does not create a copy.

Version: C++23; repository compiler flag, production runtime not attested

Syntax

*pointer_expression

The built-in unary * operator performs indirection, also called dereferencing. For a pointer of type T*, the expression *p is an lvalue of type T denoting the pointed-to object or function. Merely obtaining this lvalue does not copy the object's value. You cannot dereference void* this way.

If p points to a modifiable int object, *p = 12; changes that object, not the pointer. Through const int*, you can read the value but cannot change it. In the example, &value obtains the address of value, and *p yields an lvalue denoting that variable. In std::cout << *p, stream insertion uses the value obtained from that lvalue by lvalue-to-rvalue conversion. In *p = 12;, the assignment writes 12 to the same variable.

Distinguish three uses of the symbol: *p dereferences a pointer, a * b multiplies two values, and int* p declares a pointer—the star in a declaration does not perform dereferencing. For class types, operator* can be overloaded; its result and requirements then depend on the selected function, not solely on the built-in pointer rules.

Before reading or writing through a pointer, ensure that it points to a suitable, live object. Do not dereference nullptr, an uninitialized pointer, or a pointer one past the end of an array; do not use a dangling pointer to read or write. Checking p != nullptr alone does not establish pointer validity. Undefined behavior need not cause a crash.

The example targets C++23 compilation mode. The displayed code was run unchanged in an isolated environment, and its standard output matched the expected output exactly. The example was not verified in a production runtime. The supplied source is an evolving standard draft, not a frozen C++23 publication.

Examples

#include <iostream>

int main() {
    int value = 7;
    int* p = &value;
    std::cout << *p << '\n';
    *p = 12;
    std::cout << value << '\n';
}

Result

7
12

The expected output is `7` on the first line and `12` on the second. The first line reads `value` through `*p`; the second shows that `*p = 12;` changed `value` itself.

Sources