- Introduction
- Simple use of pointers
- Pointers and arrays
- Allocation of arrays using pointers
- Exercises

The increased security is obtained not only through that each
variable, which shall be used as a pointer, must be given an attribute
`POINTER`, but also that all variables, that will be pointed to, must be
given an attribute `TARGET`. An example explaining how to do this
follows.

REAL, TARGET :: B(10,10) REAL, POINTER :: A(:,:) A => BThe matrix

ALLOCATE (A(5,5)) DEALLOCATE (A)There is also an internal function

IF ( ASSOCIATED (A) ) WRITE(*,*) ' A is associated ' IF ( ASSOCIATED (A, B) ) WRITE(*,*) ' A is associated with B' NULLIFY (A)Please remember that a pointer in Fortran 90 has both type and rank, and that these must agree with the corresponding target. This increases the security at the use of pointers, it is therefore not possible by mistake to let a pointer change values of variables of other (different) data types. The fact that you have to specify that a variable can be a target also increases both security and efficiency of the compilation.

Important application of pointers are lists and trees, and especially dynamic arrays.

REAL, TARGET :: A REAL, POINTER :: P, Q A = 3.1416 P => A Q => P A = 2.718 WRITE(*,*) QHere the value of

REAL, TARGET :: A, B REAL, POINTER :: P, Q A = 3.1416 B = 2.718 P => A Q => BNow both the values of

Q = Pall four variables will get the value 3.1416, which means that an ordinary assignment of pointer variables has the same effect as the conventional assignment

B = AIf we instead give a pointer association

Q => Pthen the three variables

REAL, TARGET :: B(10,10) REAL, POINTER :: A(:), C(:) A => B(4,:) ! vector A becomes the fourth row C => B(:,4) ! and vector C becomes the fourth ! column of the matrix BIt is not necessary to take the whole section, you can take only a partial section. In the following example you can take a partial matrix

REAL, TARGET :: MATRIX(100,100) REAL, POINTER :: WINDOW(:,:) INTEGER :: N1, N2, M1, M2 WINDOW => MATRIX(N1:M1, N2:M2)If you later wish to change a dimension of the partial matrix

There does not exist arrays of pointers directly in Fortran 90, but
you can construct such facilities by creating a new data type. An
example is to store a lower (or left) triangular matrix with rows with
varying length. First introduce a new data type `ROW `

TYPE ROW REAL, POINTER :: R(:) END TYPEand then specify the two lower triangular matrices

INTEGER :: N TYPE(ROW) :: V(N), L(N)after which you can allocate the matrix

DO I = 1, N ALLOCATE (V(I)%R(1:I)) ! Various length of rows END DOThe statement

V = Lthen becomes equivalent with

V(I)%R => L(I)%Rfor all the components, i.e. all values of

An alternative method has however been suggested by *Arie ten Cate*,
using a module with an `ALLOCATE`d and `SAVE`d array. An
example
is available.

We however use an `INTERFACE ` with pointers in the main program and
allocate, also using pointers, a vector in the subroutine. In
this way we get a dynamically allocated vector.

PROGRAM MAIN_PROGRAM INTERFACE SUBROUTINE SUB(B) REAL, DIMENSION (:), POINTER :: B END SUBROUTINE SUB END INTERFACE REAL, DIMENSION (:), POINTER :: A CALL SUB(A) ! Now we can use the vector A. ! Its dimension was determined in the subroutine, ! the number of elements is available as SIZE(A). END PROGRAM MAIN_PROGRAM SUBROUTINE SUB(B) REAL, DIMENSION (:), POINTER :: B INTEGER M ! Now we can assign a value to M, for example ! through an input statement. ! When M has been assigned we can allocate B ! as a vector. ALLOCATE (B(M)) ! Now we can use the vector B. END SUBROUTINE SUBNote: The method above is even more useful for allocating matrices, see exercise 12.3.

Solution.

(12.2) Specify two pointers, and let one of them point to a whole
vector and the other one point to the seventh element of the same
vector.

Solution.

(12.3) Use pointers to specify a matrix in such a way, that it is
given its size (its extent) in a subroutine but can be used in the
main program.

Solution.

Last modified: 7 September 1998