10 Pointer

Pointers are available in FORTRAN 95. They represent a variable with POINTER attribute and can

Pointers are no independent data type and do not contain an address of a memory location. They can be regarded as a dynamically associated alias of a data object (the target) with TARGET attribute. A pointer permits access to the content of the variable he is pointing to.
Pointer may be used for

10.1 Definition

Pointer and target variables are declared in a type declaration statement specifying the corresponding attribute.

''type'', POINTER, [,attribute [, ...]]

var_list
type, TARGET, [,attribute [, ...]] :: var_list

type Data type
attribute Attribute of the variables
var_list List of variable-names

Type and rank (number of dimensions) of a pointer variable must be identical with those of the target variable(s) it will point to. For arrays the rank is specified by a ':' for each dimension (assumed shape). Pointers can be components of derived data types.

Please see Example 1

The pointer status is not defined initially. The intrinsic function nullify explicitly removes the association between pointer and target.

The intrinsic function ASSOCIATED returns the association status (.true. or .false.) of a pointer.

Please do Tasks 10.1, 10.2

10.2 Assigning a pointer

Initially the pointer is not associated with a target. The association is carried out with the => instruction (or by the ALLOCATE-statement for dynamically allocated variables). If a variable is assigned to a pointer, an access to the contents of the (target) variable is possible by use of either the (target) variable name or by the pointer variable.
After releasing the memory allocated by an (allocatable) array, the pointer is not any longer associated to that array.

Please see Example 2

Equating two pointers effects their targets i.e. the content is affected. The two pointers will not point to the same target!

Please see Example 3

Please do Task 10.3

10.3 Arrays Pointers

Pointers may be used in order to work with arrays in a more comfortable way. Arrays or sub-arrays may be addressed by pointers (with a modified index rage). For this the target (array) must have the TARGET attribute. The pointer can point to an array or sub-array of same type and shape. The index ranges for the pointer to an array is identical with those of the target or may have the lower boundary of 1 (for all the dimensions.)

Please see Example 4

Please see Example 5

Please do Tasks 10.4 and 10.5

10.4 Pointer on allocatable arrays

Pointer may be used for defining unnamed variables or arrays which are declared allocatable. It is carried out by applying the ALLOCATE statement with POINTER arguments. The DEALLOCATE statement releases assigned memory and nullifies the association.

Please see Example 6

10.5 Pointer arguments

A Pointer can become an argument in a procedure call, even if it is not associated to a target. The (dummy) (pointer-) argument within the procedure must have the same type and rank but no INTENT attribute has to be specified. An INTERFACE block must be specified, if a procedure is used that has at least one argument with pointer or target attribute. If an argument in a procedure call has attribute pointer but the corresponding (dummy) procedure argument has not, the argument will be associated to the target variable the pointer points to.

Please see Example 7

Please do Task 10.6

10.7 Array of Pointers

In FORTRAN 95 no array of pointers exist. However, it is possible to declare a (pseudo-) array of pointers by use of a derived data type (with a pointer component).

Please see Example 9

10.7 Array of Pointers

In FORTRAN 95 no array of pointers exist. However, it is possible to declare a (pseudo-) array of pointers by use of a derived data type (with a pointer component).

Please see Example 9

10.8 Dynamical Structures

Pointers can be used to define dynamical structure like linked lists or binary trees.
Linked lists consist of objects of a derived data type which have a pointer component. That pointer points to the next (or previous) list element.

Linked list have the following properties:

Please see Example 10

Please do Task 10.7

LehreWiki: Chapter_10 (last edited 2010-03-31 08:19:16 by HansLuthardt)