The Queen's University of Belfast
QUBPCCParallel Computer Centre

[Next] [Previous] [Top]

Pointers


Pointers

Topics

What is a pointer?

A pointer variable has the POINTER attribute and may point to:

Specifications

type [[,attribute]... ::] list of variables

REAL, DIMENSION(20), POINTER :: p ! illegal REAL, DIMENSION(:), POINTER :: p ! legal

Pointer assignment

=>

REAL, POINTER :: p1, p2
REAL, TARGET :: t1 = 3.4, t2 = 4.5
p1 => t1
PRINT *, t1, p1 ! all 3.4
p2 => t2
PRINT *, t2, p2 ! all 4.5

p2 => p1
PRINT *, t1, p1, p2 ! all 3.4

t1 = 5.2
PRINT *, t1, p1, p2 ! all 5.2

Pointer assignment

Vs ordinary assignment

REAL, POINTER :: p1, p2
REAL, TARGET :: t1 = 3.4, t2 = 4.5
p1 => t1
PRINT *, t1, p1 ! all 3.4
p2 => t2
PRINT *, t2, p2 ! all 4.5

p2 = p1 ! Equivalent to t2 = t1
! So p1 = p2 = t1 = t2 = 3.4
PRINT *, t1, t2, p1, p2 ! all 3.4

Array pointers

REAL, DIMENSION (:), POINTER :: pv1
REAL, DIMENSION (-3:5), TARGET :: tv1
pv1 => tv1 ! pv1 aliased to tv1

pv1 => tv1(:) ! pv1 points to tv1 with
! section subscript

pv1 => tv1(1:5:2) ! pv1 points to a section of
! tv1 with subscript triplet

REAL, DIMENSION (:), POINTER :: pv1
REAL, DIMENSION (:, :), POINTER :: pv2
REAL, DIMENSION (5, 10), TARGET :: tv
pv1 => tv(4, :) ! pv1 aliased to the 4th row of tv

pv2 => tv(2:4, 4:8)

Pointer status

REAL, POINTER :: p ! p undefined
REAL, TARGET :: t
PRINT *, ASSOCIATED (p) ! not valid
NULLIFY (p)
PRINT *, ASSOCIATED (p) ! .FALSE.
p => t
PRINT *, ASSOCIATED (p) ! .TRUE.

Dynamic storage

ALLOCATE and DEALLOCATE

REAL, POINTER :: p
REAL, DIMENSION (:, :), POINTER :: pv
INTEGER :: m, n
...
ALLOCATE (p, pv(m, n))
DEALLOCATE (pv) ! pv is in null status

Danger!

REAL, POINTER :: p1, p2
ALLOCATE (p1)
p1 = 3.4
p2 => p1
...
DEALLOCATE (p1)
REAL, DIMENSION(:), POINTER :: p
...
ALLOCATE(p(1000))
...
NULLIFY(p) ! nullify p without first deallocating it

Pointer arguments

Example

... ! calling program unit
INTERFACE ! do not forget interface
SUBROUTINE sub2(b)
REAL, DIMENSION(:, :), POINTER :: b
END SUBROUTINE sub2
END INTERFACE
REAL, DIMENSION(:, :), POINTER :: p
...
ALLOCATE (p(50, 50))
CALL sub1(p) ! both sub1 and sub2
CALL sub2(p) ! are external procedures
...

SUBROUTINE sub1(a) ! a is not a pointer
REAL, DIMENSION(:, :) :: a
...
END SUBROUTINE sub1

SUBROUTINE sub2(b) ! b is a pointer
REAL, DIMENSION(:, :), POINTER :: b
...
END SUBROUTINE sub2

Pointer functions

Example

...
IMPLICIT NONE
INTEGER, DIMENSION(100) :: x
INTEGER, DIMENSION(:), POINTER :: p
...
p => gtzero(x)
...
CONTAINS
! function to get all values .gt. 0 from a
FUNCTION gtzero(a)
INTEGER, DIMENSION(:), POINTER :: gtzero
INTEGER, DIMENSION(:) :: a
INTEGER :: n
... ! find the number of values .gt. 0, n
ALLOCATE (gtzero(n))
... ! put the found values into gtzero
END FUNCTION gtzero
...
END

Arrays of pointers

REAL, DIMENSION(20), POINTER :: p ! illegal TYPE real_pointer
REAL, DIMENSION(:), POINTER :: p
END TYPE real_pointer
TYPE(real_pointer), DIMENSION(100) :: a
INTEGER :: i
...
! possible to refer to the ith pointer by a(i)%p
DO i = 1, 100
ALLOCATE (a(i)%p(i))
END DO

Linked list

TYPE node
INTEGER :: value ! data field
TYPE (node), POINTER :: next ! pointer field
END TYPE node

Example

...
TYPE node
INTEGER :: value ! data field
TYPE (node), POINTER :: next ! pointer field
END TYPE node

INTEGER :: num
TYPE (node), POINTER :: list, current
NULLIFY(list) ! initially nullify list (empty) DO
READ *, num ! read num from keyboard
IF (num == 0) EXIT ! until 0 is entered
ALLOCATE(current) ! create new node
current%value = num
current%next => list ! point to previous one
list => current ! update head of list
END DO
...

If, for example, the values 1, 2, 3 are entered in that order, the list looks like (progressively):


[Next] [Previous] [Top]
All documents are the responsibility of, and copyright, © their authors and do not represent the views of The Parallel Computer Centre, nor of The Queen's University of Belfast.
Maintained by Alan Rea, email A.Rea@qub.ac.uk
Generated with CERN WebMaker