HP Fortran Programmer's Reference (September 2007)

HP Fortran statements
POINTER (statement and attribute)
Chapter 10 429
Description
A POINTER attribute or statement specifies that the named variables may be pointers to some
target object. Pointers provide a capability for creating dynamic objects, such as
dynamic-sized arrays and linked lists. An object with a pointer attribute initially has no space
reserved for its target. A pointer is assigned space for its target when an ALLOCATE statement
is executed or when it is assigned to point to a target using a pointer assignment statement.
Examples
In the first example, two array pointers are declared and used.
! Extents are not specified; they are determined during execution
REAL, POINTER :: weight (:,:,:)
REAL, POINTER :: w_reg (:,:,:)
READ *, i, j, k
ALLOCATE (weight (i, j, k)) ! create weight
! w_reg is an alias for an array section
w_reg => weight (3:i-2, 3:j-2, 3:k-2)
avg_w = sum (w_reg) / ((i-4) * (j-4) * (k-4))
DEALLOCATE (weight) ! weight no longer needed
The next example illustrates the use of pointers in a list-processing application.
TYPE link
REAL value
TYPE (link), POINTER :: next
END TYPE link
TYPE(link), POINTER :: list, save_list
NULLIFY (list) ! Initialize list
DO
READ (*, *, IOSTAT = no_more) value
IF (no_more /= 0) EXIT
save_list => list
ALLOCATE (list) ! Add link to head of list
list % value = value
list % next => save_list
END DO
! Linked list removed when no longer needed
DO
IF (.NOT.ASSOCIATED (list) ) EXIT
save_list => list % next
DEALLOCATE (list)
list => save_list
END DO