4.2.3 Pointer Assignments

In ordinary assignment involving pointers, the pointer is an alias for its target. In pointer assignment, the pointer is associated with a target. If the target is undefined or disassociated, the pointer acquires the same status as the target. The pointer assignment statement has the following form:

pointer-object => target

pointer-object
Is a variable name or structure component declared with the POINTER attribute.

target
Is a variable or expression. Its type and kind parameters, and rank must be the same as pointer-object. It cannot be an array section with a vector subscript.

Rules and Behavior

If the target is a variable, it must have the POINTER or TARGET attribute, or be a subobject whose parent object has the TARGET attribute.

If the target is an expression, the result must be a pointer.

If the target is not a pointer (it has the TARGET attribute), the pointer object is associated with the target.

If the target is a pointer (it has the POINTER attribute), its status determines the status of the pointer object, as follows:

A pointer must not be referenced or defined unless it is associated with a target that can be referenced or defined.

When pointer assignment occurs, any previous association between the pointer object and a target is terminated.

Pointers can also be assigned for a pointer structure component by execution of a derived-type intrinsic assignment statement or a defined assignment statement.

Pointers can also become associated by using the ALLOCATE statement to allocate the pointer.

Pointers can become disassociated by deallocation, nullification of the pointer (using the DEALLOCATE or NULLIFY statements), or by reference to the NULL intrinsic function.

Examples

The following are examples of pointer assignments:

HOUR => MINUTES(1:60)         ! target is an array
M_YEAR => MY_CAR%YEAR         ! target is a structure component
NEW_ROW%RIGHT => CURRENT_ROW  ! pointer object is a structure component
PTR => M                      ! target is a variable
POINTER_C => NULL ()          ! reference to NULL intrinsic

The following example shows a target as a pointer:

INTEGER, POINTER :: P, N
INTEGER, TARGET :: M
INTEGER S
M = 14
N => M                         ! N is associated with M
P => N                         ! P is associated with M through N
S = P + 5

The value assigned to S is 19 (14 + 5).

For More Information:


Previous Page Next Page Table of Contents