Pointer assignment

The pointer assignment statement causes a pointer to become associated with a target or causes the pointer's association status to become disassociated or undefined.

Read syntax diagramSkip visual syntax diagram>>-pointer_object-- => --target--------------------------------><
 
target
is a variable or expression. If it is a variable, it must have the TARGET attribute (or be a subobject of such an object) or the POINTER attribute. If it is an expression, it must yield a value that has the POINTER attribute.
pointer_object
must have the POINTER attribute.

A target must not be an array section with a vector subscript, nor can it be a whole assumed-size array.

The size, bounds, and shape of the target of a disassociated array pointer are undefined. No part of such an array can be defined or referenced, although the array can be the argument of an intrinsic inquiry function that is inquiring about association status, argument presence, or a property of the type or type parameters.

IBM Extension

A pointer of type byte can only be associated with a target of type byte, INTEGER(1), or LOGICAL(1).

End of IBM Extension

Any previous association between a pointer_object and a target is broken. If target is not a pointer, pointer_object becomes associated with target. If target is itself an associated pointer, pointer_object is associated with the target of target. If target is a pointer with an association status of disassociated or undefined, pointer_object acquires the same status. If target of a pointer assignment is an allocatable object, it must be allocated.

Pointer assignment for a pointer structure component can also occur via execution of a derived-type intrinsic assignment statement or a defined assignment statement.

During pointer assignment of an array pointer, the lower bound of each dimension is the result of the LBOUND intrinsic function applied to the corresponding dimension of the target. For an array section or array expression that is not a whole array or a structure component, the lower bound is 1. The upper bound of each dimension is the result of the UBOUND intrinsic function applied to the corresponding dimension of the target.

Related information:

Examples of pointer assignment

TYPE T
  INTEGER, POINTER :: COMP_PTR
ENDTYPE T
TYPE(T) T_VAR
INTEGER, POINTER :: P,Q,R
INTEGER, POINTER :: ARR(:)
BYTE, POINTER :: BYTE_PTR
LOGICAL(1), POINTER :: LOG_PTR
INTEGER, TARGET :: MYVAR
INTEGER, TARGET :: DARG(1:5)
P => MYVAR               ! P points to MYVAR
Q => P                   ! Q points to MYVAR
NULLIFY (R)              ! R is disassociated
Q => R                   ! Q is disassociated
T_VAR = T(P)             ! T_VAR%COMP_PTR points to MYVAR
ARR => DARG(1:3)
BYTE_PTR => LOG_PTR
END