HP Fortran Programmer's Reference (September 2007)

Data types and data objects
Pointers
Chapter 5 133
A pointer example
The example below, ptr_sts.f90, illustrates different pointer operations, including calls to the
ASSOCIATED intrinsic to determine pointer status.
Example 5-4 ptr_sts.f90
PROGRAM main
! This program performs simple pointer operations, including
! calls to the ASSOCIATED intrinsic to determine status.
!
! Declare pointer as a deferred shape array with POINTER
! attribute.
REAL, POINTER :: ptr(:)
REAL, TARGET :: tgt(2) = (/ -2.2, -1.1 /) ! initialize target
PRINT *, "Initial status of pointer:"
call get_ptr_sts
ptr => tgt ! pointer assignment
PRINT *, "Status after pointer assignment:"
call get_ptr_sts
PRINT *, "Contents of target by reference to pointer:", ptr
! use an array constructor to assign to tgt by reference to ptr
ptr = (/ 1.1, 2.2 /)
PRINT *, “Contents of target after assignment to pointer:”, tgt
NULLIFY(ptr)
PRINT *, "Status after pointer is nullified:"
call get_ptr_sts
ALLOCATE(ptr(5)) ! allocate pointer
PRINT *, "Status after pointer is allocated:"
! To learn if pointer is allocated, call the ASSOCIATED
! intrinsic without the second argument
IF (ASSOCIATED(ptr)) PRINT *, " Pointer is allocated."
ptr = (/ 3.3, 4.4, 5.5, 6.6, 7.7 /) ! array assignment
PRINT *, ‘Contents of array pointer:’, ptr
DEALLOCATE(ptr)
PRINT *, “Status after array pointer is deallocated:"
IF (.NOT. ASSOCIATED(ptr)) PRINT *, " Pointer is deallocated."
CONTAINS
! Internal subroutine to test pointer’s association status.
! Pointers can be passed to a procedure only if its interface