Pointers

In Fortran, a pointer is an alias for another item of data (or a block in the form of an array, a section of an array, or a structure). It is not the address of the item but rather it is an alternative name for the item. This association between a pointer and what it is an alias for, can be changed under program control. As a result, pointers can be used to create structures such as linked lists and trees.

 

Declaring a pointer

A pointer is declared by giving it a type with the POINTER attribute. The data item for which it is an alias must also be given the TARGET attribute. For example:

REAL,TARGET::x
REAL,POINTER::AltX

 

Associating a pointer with a target

The combination "=>" is used to associate a pointer with a given item of data. For example:

AltX=>x
AltX=1.0

makes AltX an alias for x and then assigns the value 1.0 to x.

 

The states of a pointer

A pointer is in one of three states:

  1. It may be undefined. It has been declared but not associated with an item of data.

  2. It may be null. This is a zero state obtained using NULLIFY or the intrinsic function NULL. It is used to explicitly state that the pointer is currently not an alias for any data item (e.g. a terminator for a linked list).

  3. It may be associated. It is currently an alias for some target.

The intrinsic function ASSOCIATED may be used to determine the current state of a pointer.

 

 

Basket
Empty
 
Copyright © 1999-2024 Silverfrost Limited