The Queen's University of Belfast
Parallel Computer Centre

[Next] [Previous] [Top]

Pointer Variables


Pointer variables

Topics

What are Pointers?

Pointers and targets

Pointer variables (or simply pointers) are a new type of variable.

Specifications

Form

type [, attr] :: name

INTEGER, TARGET :: list(100)

INTEGER, POINTER :: first, current, last

REAL, POINTER, DIMENSION(:) :: p(:)

REAL, TARGET :: a(3), b(6), c(9)

Pointer assignment

= and =>

INTEGER, POINTER :: pt

INTEGER, TARGET :: x=34, y=0

pt => x ! pt points to x

y = pt ! y equals x

pt => y ! pt points to y

pt = 17 ! y equals 17

Example

Dereferencing

Dereferencing - using the target data rather than value of the pointer itself - is automatic:

y = pt !y = target

pt = 17 !target = 17

IF( pt<0 ) THEN !target < 0

...

pt => y

WRITE(6,*) pt !output is y

READ(5,*) pt !input into y

A pointer must be associated with a target to be dereferenced.

Pointer status

A pointer may be in one of three states:

It is an error to use a pointer while in the disassociated or undefined state.

It is an error to test the status of an undefined pointer.

Example

REAL, POINTER :: pt !undefined

REAL, TARGET :: t1, t2

...

test = ASSOCIATED( pt ) !illegel

pt => t1

test = ASSOCIATED( pt ) !t

test = ASSOCIATED( pt, t1 ) !t

test = ASSOCIATED( pt, t2 ) !f

...

NULLIFY( pt )

test = ASSOCIATED( pt ) !f

Dynamic storage

ALLOCATE and DEALLOCATE

INTEGER, POINTER :: p, pa(:)

...

ALLOCATE( p, pa(100) )

p = 100

pa = 0

...

DEALLOCATE( pa, p) !undefined

Common pointer errors

Array pointers

Array pointers act as dynamic aliases to array sections:

REAL, TARGET :: grid(10,10)

REAL, POINTER :: centre(:,:), row(:)

centre => grid(4:7,4:7)

row => grid(9,:)

centre => grid(5:6,5:6)

Derived data types

Pointer components

TYPE data

REAL, POINTER :: a(:)

END TYPE data

TYPE(data) :: event(3)

DO i=1, 3

READ(5,*) n

ALLOCATE( event(i)%a(n) )

READ(5,*) event(i)%a

END DO

Linked lists

Pointers may point at other members of the same derived data type to create linked lists.

TYPE list

REAL :: item

TYPE( list ), POINTER :: next

END TYPE list

Note - recursion-like property in declaration allowing the pointer to reference its own data type.

Pointer arguments

Pointers may be passed as arguments to procedures.

Example

INTERFACE

SUBROUTINE suba( a )

REAL, POINTER :: a(:)

END SUBROUTINE suba

END INTERFACE

REAL, POINTER :: pt(:)

REAL, TARGET :: data(100)

...

pt => data

CALL suba( pt )

CALL subb( pt )

...

SUBROUTINE subb( b ) !internal

REAL :: b(:) !size=100

...

SUBROUTINE suba( a ) !external

REAL, POINTER :: a(:)

...

Pointer functions

Functions may return pointer(s) as a result.

Example

INTERFACE

FUNCTION max_row ( a )

REAl, TARGET :: a(:,:)

REAL, POINTER :: max_row(:)

END FUNCTION max_row

END INTERFACE

REAL, TARGET :: a(3,3)

REAL, POINTER :: p(:)

...

p => max_row ( a )

...

FUNCTION max_row ( a )

REAL, TARGET :: a(:,:)

REAL, POINTER :: max_row(:)

INTEGER :: location(2)

location = MAXLOC( a )

max_row => a(location(1),:)

END FUNCTION max_row


[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