CERN Accelerating science

This website is no longer maintained. Its content may be obsolete. Please visit http://home.cern/ for current CERN information.



next up previous
Next: Association Up: Fortran 90 Tutorial: Previous: Fortran 90 Tutorial:

Basics

Pointers are variables with the POINTER attribute; they are not a distinct data type (and so no ``pointer arithmetic'' is possible):

          REAL, POINTER :: var
They are conceptually a descriptor listing the attributes of the objects (targets) that the pointer may point to, and the address, if any, of a target. They have no associated storage until it is allocated or otherwise associated (by pointer assignment, see below):

          ALLOCATE (var)
and they are dereferenced automatically, so no special symbol is required. In

                  var = var + 2.3
the value of the target of var is used and modified. Pointers cannot be transferred via I/O---the statement

                 WRITE *, var
writes the value of the target of var and not the pointer descriptor itself.

A pointer can point to other pointers, and hence to their targets, or to a static object that has the TARGET attribute:

          REAL, POINTER :: object
          REAL, TARGET  :: target_obj
          var => object                  ! pointer assignment
          var => target_obj
but they are strongly typed:

          INTEGER, POINTER :: int_var
          var => int_var                 ! illegal - types must match
and, similarly, for arrays the ranks as well as the type must agree.

A pointer can be a component of a derived type:

        TYPE entry                       ! type for sparse matrix
           REAL value
           INTEGER index
           TYPE(entry), POINTER :: next  ! note recursion
        END TYPE entry
and we can define the beginning of a linked chain of such entries:

        TYPE(entry), POINTER :: chain
After suitable allocations and definitions, the first two entries could be addressed as

        chain%value           chain%next%value
        chain%index           chain%next%index
        chain%next            chain%next%next
but we would normally define additional pointers to point at, for instance, the first and current entries in the list.



Janne Saarela
Tue May 16 13:43:26 METDST 1995