Skip to main content Skip to navigation

8. Pointers

Pointers are a very useful device in f90. They are used in a wide variety of instances - most importantly in managing memory requirements. This is just a quickstart guide to intoduce you to pointers, so you can recognise them, and use them in a few simple applications. This section is not intended to be a comprehensive resource - you should read and practice more to appreciate pointers fully.

1. Introduction

A pointer is an object that "points" to a memory address of another object (say, a variable). It's like the index of a book - you go to the index, and look up, say, "cricket". The index will tell you on which page (e.g. like a memory address) you can find a mention of cricket.

Pointers must be declared, and the declaration format is very similar to what you have already seen, except that statement pointer must be used in the declaration e.g. the general syntax for declaration is

type, pointer :: pointer_name


Below shows two pointers declared on one line:
integer, pointer :: tag1, tag2

If you want your pointer to point at a variable, array etc, then you need to declare this variable/array as a target. You can declare target variables using the general syntax:,

type, target :: variable_name

The rank of a target and pointer are fixed, but the size of the target (shape) may vary. The type of the target and pointer must also match.

There are 3 main states a pointer can exist in: associated (when it is pointing at a particular target), disassociated (when it is not pointing at a target) and undefined (the default initial status). Therefore you should always initilise your pointers by setting them to null, eg

integer, pointer :: tag1 = null()


Below shows targets and pointers declared :
real, target :: a, b(1000),ag
integer, target :: i, j(100),c(20,20)
real, pointer :: tag1, aptr, pb(:), pc1(:)
integer, pointer :: ipb, ipc(:), ipd(:,:)

You can associate a pointer with a target with an assignment statement as shown below:

tag1 => a

This now points the pointer tag1 to point at variable a. Following this assignment statement you can use a regular assignment such as:

b(i) = tag1*c(i,i)

This is in effect identical with the statement

b(i) = a*c(i,i)

Regular assignment (using the equals sign) is also possible using pointers. In this case, it assigns a value to a space pointed at by the pointer


Below shows the difference between regular and pointer assignment :
ag = 5.0
tag1 = ag

ag = 2.3
After the second statement, a and tag1 have the same value
tag1 is now like an 'alias' for a, so the third statement sets the value of a = 5.0
if ag is changed, the value of tag1 and a do not change (fourth statement)
in the last statement, this newly associates tag1 with a new target
Back to top

2. Dynamic memory allocation

Pointers give us a means of allocating memory for a program at run time rather than at compile time - this is called dynamic memory allocation. This can be done by dynamically allocating space as the target of a pointer:


To allocate memory at run time for a single real number using pointers:
To allocate memory at run time for a linear array using pointers (using an integer n):
Back to top