HP Fortran Programmer's Reference (September 2007)

Expressions and assignment
Assignment
Chapter 4100
If
target-expression
is a pointer already associated with a target, then
pointer-object
becomes associated with the target of
target-expression
. If
target-expression
is a
pointer that is disassociated or undefined, then
pointer-object
inherits the disassociated or
undefined status of
target-expression
. For information about pointer status, see “Pointer
association status” on page 132.
The following example, ptr_assign.f90, illustrates association of scalar and array pointers
with scalar and array targets:
Example 4-1 ptr_assign.f90
PROGRAM main
INTEGER, POINTER :: p1, p2, p3(:) ! declare three pointers, p3
! is a deferred-shape array
INTEGER, TARGET :: t1 = 99, t2(5) = (/ 1, 2, 3, 4, 5 /)
! p1, p2 and p3 are currently undefined.
p1 => t1 ! p1 is associated with t1.
PRINT *, 'contents of t1 referenced through p1:', p1
p2 => p1 ! p2 is associated with t1.
! p1 remains associated with t1.
PRINT *, 'contents of t1 referenced through p1 through p2:', p2
p1 => t2(1) ! p1 is associated with t2(1).
! p2 remains associated with t1.
PRINT *, 'contents of t2(1) referenced through p1:', p1
p3 => t2 ! p3 is associated with t2.
PRINT *, &
'contents of t2 referenced through the array pointer p3:', p3
p1 => p3(2) ! p1 is associated with t2(2).
PRINT *, &
'contents of t2(2) referenced through p3 through p1:', p1
NULLIFY(p1) ! p1 is disassociated.
IF (.NOT. ASSOCIATED(p1)) PRINT *, "p1 is disassociated."
p2 => p1 ! Now p2 is also disassociated.
IF (.NOT. ASSOCIATED(p2)) PRINT *, &
"p2 is disassociated by pointer assignment."
END PROGRAM main
Here are the command lines to compile and execute the program, along with the output from
a sample run:
$ f90 ptr_assign.f90
$ a.out
contents of t1 referenced through p1: 99