POINTER - Fortran 95/90

Statement and Attribute: Specifies that an object is a pointer (a dynamic variable). A pointer does not contain data, but points to a scalar or array variable where data is stored. A pointer has no initial storage set aside for it; memory storage is created for the pointer as a program runs.

Syntax

The POINTER attribute can be specified in a type declaration statement or a POINTER statement, and takes one of the following forms:

Type Declaration Statement:

type,[att-ls,] POINTER [, att-ls] :: ptr[(d-spec)][ , ptr[(d-spec)]]...

Statement:

POINTER [::]ptr[(d-spec)][ , ptr[(d-spec)]] ...

type-spec

Is a data type specifier.

att-ls

Is an optional list of attribute specifiers.

ptr

Is the name of the pointer. The pointer cannot be declared with the INTENT or PARAMETER attributes.

d-spec

(Optional) Is a deferred-shape specification (: [, :] ...). Each colon represents a dimension of the array.

Description

No storage space is created for a pointer until it is allocated with an ALLOCATE statement or until it is assigned to a allocated target. A pointer must not be referenced or defined until memory is associated with it.

Each pointer has an association status, which tells whether the pointer is currently associated with a target object. When a pointer is initially declared, its status is undefined. You can use the ASSOCIATED intrinsic function to find the association status of a pointer if the pointer's association status is defined.

If the pointer is an array, and it is given the DIMENSION attribute elsewhere in the program, it must be declared as a deferred-shape array.

A pointer cannot be specified in an EQUIVALENCE or NAMELIST statement. A pointer in a DATA statement can only be associated with NULL().

Fortran 95/90 pointers are not the same as integer pointers. For more information, see the POINTER - Integer statement.

Example

The following example shows type declaration statements specifying the POINTER attribute:

TYPE(SYSTEM), POINTER :: CURRENT, LAST

REAL, DIMENSION(:,:), POINTER :: I, J, REVERSE

The following is an example of the POINTER statement:

TYPE(SYSTEM) :: TODAYS

POINTER :: TODAYS, A(:,:)

See also the examples POINTER.F90 and POINTER2.F90in the TUTORIAL sample programs.

The following shows another example:

REAL, POINTER :: arrow (:)

REAL, ALLOCATABLE, TARGET :: bullseye (:,:)

! The following statement associates the pointer with an unused

! block of memory.

ALLOCATE (arrow (1:8), STAT = ierr)

IF (ierr.eq.0) WRITE (*,'(/1x,a)') 'ARROW allocated'

arrow = 5.

WRITE (*,'(1x,8f8.0/)') arrow

ALLOCATE (bullseye (1:8,3), STAT = ierr)

IF (ierr.eq.0) WRITE (*,*) 'BULLSEYE allocated'

bullseye = 1.

bullseye (1:8:2,2) = 10.

WRITE (*,'(1x,8f8.0)') bullseye

! The following association breaks the association with the first

! target, which being unnamed and unassociated with other pointers,

! becomes lost. ARROW acquires a new shape.

arrow => bullseye (2:7,2)

WRITE (*,'(/1x,a)') 'ARROW is repointed & resized, all the 5s are lost'

WRITE (*,'(1x,8f8.0)') arrow

NULLIFY (arrow)

IF (.NOT.ASSOCIATED(arrow)) WRITE (*,'(/a/)') ' ARROW is not pointed'

DEALLOCATE (bullseye, STAT = ierr)

IF (ierr.eq.0) WRITE (*,*) 'Deallocation successful.'

END

See Also