Intel® Fortran Compiler Classic and Intel® Fortran Compiler Developer Guide and Reference

ID 767251
Date 3/22/2024
Public

A newer version of this document is available. Customers should click here to go to the newest version.

Document Table of Contents

POINTER - Fortran

Statement and Attribute: Specifies that an object or a procedure 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.

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 data pointer until it is allocated with an ALLOCATE statement or until it is assigned to a allocated target.

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.

Entities with the POINTER attribute can be associated with different data objects or procedures during execution of a program.

A data pointer must not be referenced or defined unless it is pointer associated with a target object that can be referenced or defined. A procedure pointer must not be referenced unless it is pointer associated with a target procedure.

If the data 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().

A procedure that has both the EXTERNAL and POINTER attributes is a procedure pointer.

An entity with the POINTER attribute must not have the ALLOCATABLE, INTRINSIC, or TARGET attribute, and it must not be a coarray.

Fortran 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(:,:)

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