Pointers are variables with the
they are not a distinct data type (and so no ``pointer arithmetic'' is
REAL, POINTER :: varThey 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.3the value of the target of
varis used and modified. Pointers cannot be transferred via I/O---the statement
WRITE *, varwrites the value of the target of
varand 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_objbut they are strongly typed:
INTEGER, POINTER :: int_var var => int_var ! illegal - types must matchand, 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 entryand we can define the beginning of a linked chain of such entries:
TYPE(entry), POINTER :: chainAfter 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%nextbut we would normally define additional pointers to point at, for instance, the first and current entries in the list.