Attribute Purpose

The target attribute specifies variables to be targets so that they can be accessed by variables that have the pointer attribute.

Examples and Results

Declaring variables with the target attribute is similar to declaring a variable with any other attribute. For instance, a simple declaration would like the following:
		real, target :: t1
Besides being compatible with the real type declaration, the target attribute can be used with all of the other FORTRAN data types. Also, the target attribute can be used with allocatable, dimension, intent, public, private and save attributes so that targets can be used in a wider range of applications.

To associate a pointer with a target is another relatively simple operation. This is done in the following manner
		p1 => t1
where p1 is the pointer and t1 is the target. The only thing you need to watch out for is to see if the type and attributes of the pointer and target match.

Why do we have Target Variables?

You may be left wondering from the lectures and from the above discussion exactly why we have target variables. FORTRAN is unlike C in that it does not need to pass associated pointers to functions. Besides the language survived without them for several decades. Well, one of the biggest reasons why pointers and targets were introduced in FORTRAN 90 was to save computer time. For instance, before FORTRAN 90 if you wanted to swap the values contained inside of two arrays, you had to do something like this:
		do 100 i=1,n 
		   a(i) = temp
		   a(i) = b(i)
		   b(i) = temp
	100	continue
If arrays a and b where huge arrays, then this do loop would take a long time to cycle through wasting precious computing time. However, with pointers and targets to do this swap all you need to do is the following:
		real, dimension(n), target :: a, b
		real, dimension(n), pointer :: p1, p2
		p1 => a
		p2 => b
	c	Now swap pointers
		p2 => a 
		P1 => b
This simple switch can save significant amounts of computer time and works because you can preform mathematical operations on the pointers in place of their associated target. Such switches are very common in applications solving differential equations.

There are plenty of other reasons why pointers and targets were introduced, but they are to numerous to mention here. To find out more about the potential uses of both the pointer and target attributes, read the pointer attribute definition and the lecture notes.

For additional information and examples see

The pointer attribute definition

Lecture forty two

Examples: pointers.f and associated.f

Up one level / Home

Written by Jason Wehr : and Maintained by John Mahaffy :