Pointer conversions are performed when pointers are used, including pointer assignment, initialization, and comparison.
Conversions that involve pointers must use an explicit type cast. The
exceptions to this rule are the allowable assignment conversions for C
pointers. In the following table, a const-qualified
lvalue cannot be used as a left operand of the assignment.
Table 1. Legal assignment conversions for C pointers
Left operand type | Permitted right operand types |
---|---|
pointer to (object) T |
|
pointer to (function) F |
|
The referenced type of the left operand must have the same qualifiers as the right operand. An object pointer may be an incomplete type if the other pointer has type void*.
C pointers are not necessarily the same size as type int. Pointer arguments given to functions should be explicitly cast to ensure that the correct type expected by the function is being passed. The generic object pointer in C is void*, but there is no generic function pointer.
Conversion to void*
Any pointer to an object of a type T, optionally type-qualified, can be converted to void*, keeping the same const or volatile qualifications.
The allowable assignment conversions involving void* as
the left operand are shown in the following table.
Table 2. Legal assignment conversions in C for void*
Left operand type | Permitted right operand types |
---|---|
(void*) |
|
The object T may be an incomplete type.
Pointers to functions cannot be converted to the type void* with a
standard conversion: this can be accomplished explicitly, provided that
a void* has sufficient bits to hold it.
You can convert an rvalue pointer of type B* to an rvalue pointer
of class A* where A is an accessible base class of
B as long as the conversion is not ambiguous. The conversion
is ambiguous if the expression for the accessible base class can refer to more
than one distinct class. The resulting value points to the base class
subobject of the derived class object. If the pointer of type
B* is null, it will be converted to a null pointer of type
A*. Note that you cannot convert a pointer to a class into a
pointer to its base class if the base class is a virtual base class of the
derived class.
Null Pointer Constants
A constant expression that evaluates to zero is a null pointer constant. This expression can be converted to a pointer. This pointer will be a null pointer (pointer with a zero value), and is guaranteed not to point to any object.
Array-to-Pointer Conversions
You can convert an lvalue or rvalue with type "array of N," where N is the type of a single element of the array, to N*. The result is a pointer to the initial element of the array. You cannot perform the conversion if the expression is used as the operand of the & (address) operator or the sizeof operator.
Function-to-Pointer Conversions
You can convert an lvalue that is a function of type T to an rvalue that is a pointer to a function of type T, except when the expression is used as the operand of the & (address) operator, the () (function call) operator, or the sizeof operator.
Related References