# 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. Pointer assignment has the following form:

pointer-object [ (s-spec) ] => target

pointer-object | Is a variable name or structure component declared with the POINTER attribute.
s-spec | Is a shape specification consisting of bounds information in the form "[lower-bound]:" or "[lower-bound] :upper-bound".
target | Is a variable or expression. Its type and kind parameters, and rank must be the same as pointer-object unless bounds remapping is specified. It cannot be an array section with a vector subscript.

## Description

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:

- If the pointer is associated, the pointer object is associated with the same object as the target

- If the pointer is disassociated, the pointer object becomes disassociated

- If the pointer is undefined, the pointer object becomes undefined

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.

Pointer assignment for arrays allows lower bounds to be specified. The specified lower bounds can be any scalar integer expressions.

Remapping of the elements of a rank-one array is permitted. The mapping is in array-element order and the target array must be large enough. The specified bounds may be any scalar integer expressions.

## 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).

You can use the intrinsic function ASSOCIATED to find out if a pointer is associated with a target or if two pointers are associated with the same target. For example:

      REAL C (:), D(:), E(5)
      POINTER C, D
      TARGET E
      LOGICAL STATUS
      ! Pointer assignment.
      C => E
      ! Pointer assignment.
      D => E
      ! Returns TRUE; C is associated.
      STATUS = ASSOCIATED (C)
      ! Returns TRUE; C is associated with E.
      STATUS = ASSOCIATED (C, E)
      ! Returns TRUE; C and D are associated with the
      ! same target.
      STATUS = ASSOCIATED (C, D)

The following example shows how to specify lower bounds on a pointer:

    REAL, TARGET :: A(4,4)
    POINTER P
    P (0:,0:) => A            ! LBOUND (P) == [0,0]
                              ! UBOUND (P) == [3,3]

The following example shows pointer remapping of the elements of a rank-one array:

    REAL, TARGET :: V(100)
    POINTER P
    INTEGER N
    P(1:N,1:2*N) => V(1:2*N*N)

Parent topic: Assignments

## See Also

Arrays
ALLOCATE statement
DEALLOCATE statement
NULLIFY statement
NULL intrinsic function
POINTER attribute
TARGET attribute
Defined assignments
Intrinsic Assignments for details on derived-type intrinsic assignments