Previous Table of Contents Next


1.2.19. Pointer Variables

In Fortran, a pointer variable or simply a pointer is best thought of as a free-floating name that can be associated dynamically with or aliased to some data object. The data object already may have one or more other names, or it may be an unnamed object.

Syntactically, a pointer is just any sort of variable that has been given the pointer attribute in a declaration. A variable with the pointer attribute may be used just like any ordinary variable, but it may be used in some additional ways as well.

To understand how Fortran pointers work, it is almost always better to think of them simply as aliases. Another possibility is to think of the pointers as descriptors, sufficient to describe a row of a matrix, for example.

1.2.19.1. The Use of Pointers

Each pointer in a program is in one of three states:

  It may be undefined, which is the condition of all pointers at the beginning of a program.
  It may be null, which means that it is not the alias of any data object.
  It may be associated, which means that it is the alias of some target data object.

The terms disassociated and not associated are used when a pointer is in state 1 or state 2. The associated intrinsic inquiry function distinguishes between states 2 and 3 only; its arguments must not be undefined.

1.2.19.2. The Pointer Assignment Statement

A variable with the pointer attribute may be an object more complicated than a simple variable. It may be an array or structure, for example. The following declares v to be a pointer to a one-dimensional array of reals:

   real, dimension (:), pointer :: v

With v so declared, it may be used to alias any one-dimensional array of reals, including a row or column of some two-dimensional array of reals where

   v => real_array (4, :)

makes v an alias of the fourth row of the array real_array. real_array must have the target attribute for this to be legal:

   real, dimension (100, 100), target :: real_array

Once a variable with the pointer attribute is an alias for some data object—that is, it is pointing to something—it may be used in the same way that any other variable may be used. For the preceding example, using v,

   print *, v

has the same effect as

   print *, real_array (4, :)

and the assignment statement

   v = 0

has the effect setting all the elements of the fourth row of the array real_array to 0.

1.2.19.3. The allocate and deallocate Statements

With the allocate statement, it is possible to create space for a value and cause a pointer variable to refer to that space. The space has no name other than the pointer mentioned in the allocate statement. For example, if p1 is declared by

   real, pointer :: p1

the statement

   allocate (p1)

creates space for one real number and makes p1 an alias for that space. No real value is stored in the space by the allocate statement, so it is necessary to assign a value to p1 before it can be used, just as with any other real variable.

The deallocate statement throws away the space pointed to by its argument and makes its argument null (state 2) where

   deallocate (p1)

disassociates p1 from any target and nullifies it.

1.2.19.4. The null Function

At the beginning of a program, a pointer variable (just as all other variables) is not defined unless it is initialized with the null intrinsic function. A pointer variable must not be referenced to produce a value when it is not defined, but it is sometimes desirable to have a pointer variable be in the state of not pointing to anything, which might signify the last item in a linked list, for example. This occurs when it is nullified, which creates a condition that may be tested and assigned to other pointers by pointer assignment (=>). The following statement nullifies p1:

   p1 => null ()

If p1 is null, then executing the pointer assignment

   p2 => p1

causes p2 to be null also.

1.2.19.5. The associated Intrinsic Function

The associated intrinsic function can be used to determine if a pointer variable is pointing to, or is an alias for, another object. To use this function, the pointer variable must be defined; that is, it must either be the alias of some data object or be null. The associated function indicates which of these two cases is true; thus it provides the means of testing if a pointer is null.

The associated function may have a second argument. If the second argument is a target, the value of the function indicates whether the first argument is an alias of the second argument. If the second argument is a pointer, it must be defined; in this case, the value of the function is true if both pointers are null or if they are both aliases of the same target. For example, the expression

   associated (p1, r)

indicates whether p1 is an alias of r, and the expression

   associated (p1, p2)

indicates whether p1 and p2 are both aliases of the same thing or they are both null.


Previous Table of Contents Next