Copyright (c) 1994 J. Adams, W. Brainerd, J. Martin, and B. Smith.
All rights reserved.
This file may not be copied without permission of the authors.

65 Pointers

Pointers are used to provide dynamic-data-object and aliasing capabilities in Fortran. By deferring the sizes of objects to execution time, a code can run at the exact size needed; recompilation for unusual cases is no longer required. Dynamic structures such as lists and trees can grow in ways that could not be anticipated when the program was written. The use of pointer aliasing can contribute to more readable, maintainable code.

The elements of the Fortran 90 pointer facility are: two attributes, POINTER and TARGET; four statements, NULLIFY, ALLOCATE, DEALLOCATE, and pointer assignment; and one intrinsic function, ASSOCIATED.

Examples:

REAL, POINTER :: WEIGHT (:,:,:)    ! Extents are not specified;
REAL, POINTER :: W_REGION (:,:,:)  !   they are determined
READ *, I, J, K                    !   during execution.
   . . .
ALLOCATE (WEIGHT (I, J, K))               ! WEIGHT is created.
W_REGION => WEIGHT (3:I-2, 3:J-2, 3:K-2)  ! W_REGION is an alias
                                          ! for an array section.
AVG_W = SUM (W_REGION) / ( (I-4) * (J-4) * (K-4) )
   . . .
DEALLOCATE (WEIGHT)            ! WEIGHT is no longer needed.

TYPE CATALOG
   INTEGER :: ID, PUB_YR, NO_PAGES
   CHARACTER, POINTER :: SYNOPSIS (:)
END TYPE CATALOG
   . . .
TYPE(CATALOG), TARGET :: ANTHROPOLOGY (5000)

CHARACTER, POINTER :: SYNOPSIS (:)
   . . .
DO I = 1, 5000
   SYNOPSIS => ANTHROPOLOGY(I) % SYNOPSIS ! Alias for a component
   WRITE (*,*) HEADER, SYNOPSIS, DISCLAIMER ! of an array element
   . . .
END DO

Related Topics:

ALLOCATE and DEALLOCATE Statements
Dynamic Objects
Interfaces and Interface Blocks
NULLIFY Statement
Pointer Association
POINTER Attribute and Statement
TARGET Attribute and Statement

To Read More About It:

ISO 1539 : 1991, Fortran Standard, 2.4.6, 5.1.2.7-8, 6.3, 7.5.2, 13.8.10, 13.13.13, 14.6.2, C.4.6, C.5.2-3, C.6.4, C.7.3-4, C.11.3, C.12.8, C.12.9
Fortran 90 Handbook, 0, 2.3.4, 5.4, 6.5, 7.5.3, A.13
Programmer's Guide to Fortran 90, Chapter 8


Linked List Example:

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

DO                                    ! Linked list can be
   IF (.NOT.ASSOCIATED (LIST) ) EXIT  !   removed when no
   SAVE_LIST => LIST % NEXT           !   longer needed.
   DEALLOCATE (LIST)
   LIST => SAVE_LIST
END DO

Things To Know:

  1. POINTER is an attribute in Fortran 90-not a type. An object of any type can have the POINTER attribute. Such an object cannot be referenced until it is associated with a target. A pointer target must have the same type, rank, and kind as the pointer. When the name of an object with the POINTER attribute appears in most executable statements, it is its target that is referenced.
  2. To be a candidate for a pointer target, most objects must be given the TARGET attribute; a pointer has this attribute implicitly. A target may be thought of as an object with dynamic names.
  3. When the name of an object with the POINTER attribute appears in certain places, it is the pointer that is referenced. These include the left side of a pointer assignment statement, a NULLIFY, ALLOCATE and DEALLOCATE statement, and arguments of the ASSOCIATED intrinsic function. A function may return a pointer or have pointer arguments; if so, the function must have an explicit interface.
  4. Recursive procedures are helpful in dealing with dynamic structures such as lists and trees.