cpp/language/pointer

Declares a variable of a pointer or pointer-to-member type.

Syntax
A pointer declaration is any simple declaration whose has the form

@1@ Pointer declarator: the declaration declares  as a pointer to the type determined by. @2@ Pointer to member declarator: the declaration declares  as a pointer to non-static member of  of type determined by.

There are no pointers to s and there are no pointers to. Typically, mentions of "pointers" without elaboration do not include pointers to (non-static) members.

Pointers
Every value of pointer type is one of the following:
 * a pointer to an object or function (in which case the pointer is said to point to the object or function), or
 * a pointer past the end of an object, or
 * the null pointer value for that type, or
 * an invalid pointer value.

A pointer that points to an object represents the address of the first byte in memory occupied by the object. A pointer past the end of an object represents the address of the first byte in memory after the end of the storage occupied by the object.

Note that two pointers that represent the same address may nonetheless have different values.

Indirection through an invalid pointer value and passing an invalid pointer value to a deallocation function have undefined behavior. Any other use of an invalid pointer value has implementation-defined behavior. Some implementations might define that copying an invalid pointer value causes a system-generated runtime fault.

Pointers to objects
A pointer to object can be initialized with the return value of the applied to any expression of object type, including another pointer type:

Pointers may appear as operands to the built-in indirection operator (unary ), which returns the identifying the pointed-to object:

Pointers to class objects may also appear as the left-hand operands of the member access operators and.

Because of the implicit conversion, pointer to the first element of an array can be initialized with an expression of array type:

Because of the implicit conversion for pointers, pointer to a base class can be initialized with the address of a derived class:

If is, such pointer may be used to make.

Certain, operators are defined for pointers to elements of arrays: such pointers satisfy the  requirements and allow the C++ library algorithms to work with raw arrays.

are defined for pointers to objects in some situations: two pointers that represent the same address compare equal, two null pointer values compare equal, pointers to elements of the same array compare the same as the array indexes of those elements, and pointers to non-static data members with the same compare in order of declaration of those members.

Many implementations also provide of pointers of random origin, e.g. if they are implemented as addresses within continuous virtual address space. Those implementations that do not (e.g. where not all bits of the pointer are part of a memory address and have to be ignored for comparison, or an additional calculation is required or otherwise pointer and integer is not a 1 to 1 relationship), provide a specialization of std for pointers that has that guarantee. This makes it possible to use all pointers of random origin as keys in standard associative containers such as std or std.

Pointers to void
Pointer to object of any type can be to pointer to  (optionally ); the pointer value is unchanged. The reverse conversion, which requires or, yields the original pointer value:

If the original pointer is pointing to a base class subobject within an object of some polymorphic type, may be used to obtain a  that is pointing at the complete object of the most derived type.

Pointers to void have the same size, representation and alignment as pointers to.

Pointers to void are used to pass objects of unknown type, which is common in C interfaces: std returns, std expects a user-provided callback that accepts two arguments. expects a user-provided callback that accepts and returns. In all cases, it is the caller's responsibility to cast the pointer to the correct type before use.

Pointers to functions
A pointer to function can be initialized with an address of a non-member function or a static member function. Because of the implicit conversion, the address-of operator is optional:

Unlike functions or references to functions, pointers to functions are objects and thus can be stored in arrays, copied, assigned, etc.

Note: declarations involving pointers to functions can often be simplified with type aliases:

A pointer to function can be used as the left-hand operand of the, this invokes the pointed-to function:

Dereferencing a function pointer yields the lvalue identifying the pointed-to function:

A pointer to function may be initialized from an overload set which may include functions, function template specializations, and function templates, if only one overload matches the type of the pointer (see for more detail):

are defined for pointers to functions (they compare equal if pointing to the same function).

Pointers to data members
A pointer to non-static member object which is a member of class  can be initialized with the expression  exactly. Expressions such as or  inside 's member function do not form pointers to members.

Such pointer may be used as the right-hand operand of the  and :

Pointer to data member of an accessible unambiguous non-virtual base class can be to pointer to the same data member of a derived class:

Conversion in the opposite direction, from a pointer to data member of a derived class to a pointer to data member of an unambiguous non-virtual base class, is allowed with and, even if the base class does not have that member (but the most-derived class does, when the pointer is used for access):

The pointed-to type of a pointer-to-member may be a pointer-to-member itself: pointers to members can be multilevel, and can be cv-qualified differently at every level. Mixed multi-level combinations of pointers and pointers-to-members are also allowed:

Pointers to member functions
A pointer to non-static member function which is a member of class  can be initialized with the expression  exactly. Expressions such as or  inside 's member function do not form pointers to member functions.

Such pointer may be used as the right-hand operand of the  and. The can be used only as the left-hand operand of a function-call operator:

Pointer to member function of a base class can be to pointer to the same member function of a derived class:

Conversion in the opposite direction, from a pointer to member function of a derived class to a pointer to member function of an unambiguous non-virtual base class, is allowed with and, even if the base class does not have that member function (but the most-derived class does, when the pointer is used for access):

Pointers to member functions may be used as callbacks or as function objects, often after applying std or std:

Null pointers
Pointers of every type have a special value known as null pointer value of that type. A pointer whose value is null does not point to an object or a function (the behavior of dereferencing a null pointer is undefined), and compares equal to all pointers of the same type whose value is also null.

To initialize a pointer to null or to assign the null value to an existing pointer, the null pointer literal, the null pointer constant null, or the from the integer literal with value  may be used.

and also initialize pointers to their null values.

Null pointers can be used to indicate the absence of an object (e.g. ), or as other error condition indicators (e.g. ). In general, a function that receives a pointer argument almost always needs to check if the value is null and handle that case differently (for example, the does nothing when a null pointer is passed).

Constness

 * If appears before  in the pointer declaration, it is part of  and applies to the pointed-to object.
 * If appears after  in the pointer declaration, it is part of  and applies to the pointer that's being declared.

In general, implicit conversion from one multi-level pointer to another follows the rules described in and in.