5.4. POINTER Properties

Most attributes, when applied to an object, add characteristics that the object would not have otherwise. The POINTER attribute, in some sense, removes a characteristic that an object has. Ordinarily, an object has storage space set aside. If the object has the POINTER attribute, it has no space initially and must not be referenced until space is associated with it. An ALLOCATE statement creates new space for a pointer object. A pointer assignment statement permits the pointer to borrow the space from another object. The space that becomes associated with a pointer is called the pointer's target. The target can change during the execution of a program. A pointer target is either an object or part of an object declared to have the TARGET attribute; or it is an object or part of an object that was created by the allocation of a pointer. A pointer can be assigned the target (or part of the target) of another pointer.

Another way of thinking about a pointer is as a descriptor that contains information about the type, type parameters, rank, extents, and location of the pointer's target. Thus, a pointer to a scalar object of type real is different from a pointer to an array of user-defined type.

5.4.1. POINTER Attribute and Statement

The following is a format for a type declaration statement with a POINTER attribute:

type, POINTER [, attribute_list ] :: entity_decl_list

Subject to the rules governing combinations of these attributes, attribute_list can contain the following:

AUTOMATIC (EXTENSION)
DIMENSION (with deferred shape)
OPTIONAL
PRIVATE
PROTECTED (F2003)
PUBLIC
SAVE
STATIC (EXTENSION)
VOLATILE (F2003)

The POINTER statement also provides a means for declaring pointers. Its format is defined as follows:

Table 5-10.

 

pointer_stmt

is

POINTER [ :: ] object_name [ (deferred_shape_spec_list) ]
 [ , object_name [ ( deferred_shape_spec_list) ] ] ...

The target of a pointer can be a scalar or an array.

An array pointer must be declared as a deferred-shape array.

A pointer must not be referenced or defined unless it is associated with a target that can be referenced or defined. (A pointer on the right-hand side of a pointer assignment is not considered a pointer reference.)

The POINTER statement also confers the POINTER attribute. It is subject to the same rules and restrictions as the POINTER attribute.

The following example shows an entity-oriented declaration:

TYPE(NODE), POINTER :: CURRENT
REAL, POINTER :: X(:, :), Y(:)

The following example shows an attribute-oriented declaration:

TYPE(NODE) CURRENT
REAL X(:, :), Y(:)
POINTER CURRENT, X, Y

5.4.2. TARGET Attribute and Statement

You may associate a pointer with an object that has the TARGET attribute. However, you cannot associate a pointer with an object that does not have the TARGET or POINTER attribute. If an object has the TARGET attribute, then all of its nonpointer subobjects also have the TARGET attribute. If an object does not have the TARGET attribute or has not been allocated, no part of it can be accessed through a pointer.

The following is a format for a type declaration statement with a TARGET attribute:

type, TARGET [, attribute_list ] :: entity_list

Subject to the rules governing combinations of these attributes, attribute_list can contain the following:

ALLOCATABLE
AUTOMATIC (EXTENSION)
DIMENSION
INTENT
OPTIONAL
PRIVATE
PROTECTED (F2003)
PUBLIC
SAVE
STATIC (EXTENSION)
VALUE (F2003)
VOLATILE (F2003)

The type declaration statement can also contain an initialization_expr.

The TARGET statement also provides a means for specifying pointer targets. It has the following format:

Table 5-11.

 

target_stmt

is

TARGET [ :: ] object_name [ (array_spec) ]
  [, object_name [ ( array_spec) ] ] ...

The TARGET statement also confers the TARGET attribute. It is subject to the same rules and restrictions as the TARGET attribute.

The following examples show entity-oriented declarations:

TYPE(NODE), TARGET :: HEAD_OF_LIST
REAL, TARGET, DIMENSION(100, 100) :: V, W(100)

The following examples show attribute-oriented declarations:

TYPE(NODE) HEAD_OF_LIST
REAL V, W(100)
DIMENSION V(100, 100)
TARGET HEAD_OF_LIST, V, W