POINTERS AND ARRAYS
The important thing to remember about pointers is that the indices used to reference the array will always start with one, no matter what the indices are in the actual array. The following is an assignment statement preceded by the declaration of the array pointer.
REAL, DIMENSION(:,:), POINTER ::ptoa REAL, DIMENSION(1:5,1:5), TARGET :: a ptoa=> a(3:5,::2)
The result of this assignment is that the shape of ptoa is a 3x2 matrix. When using ptoa to index the position represented in ’a’ as (3,2) one would write ptoa(1,2) since the index of pointers always starts at one. If one were to call the entire array with ptoa(1:3, 1:2), one would actually be calling the positions a(3:5,1:2). When assigning arrays to pointers it is important to maintain the same rank (dimension) as the declared pointer. This means that the following assignment statements would be illegal:
ptoa=>a(1,2) ptoa=>a(3:5,1) ptoa=>a(2,3:5)
The pointer ptoa can be assigned to a single value of the array ’a’. For example, to identify the location at (2,3),one would reference ptoa=>a(2:2,3:3). This maintains the dimension by creating a 1 × 1 two-dimensional array. This is important to remember. This characteristic is based on the manner in which data is saved in the computer’s memory,rather than the actual characteristics of the data.
POINTERS AND DERIVED TYPES
Although it is illegal to use allocatable arrays in derived types it is possible to use a combination of derived types and pointers to reference variable size arrays. The result is a derived type that in effect is an allocatable array, though not directly. This is done by creating a derived type that is a pointer and then setting the derived type variable equal to the array one desires. The result looks like
TYPE STRING CHARACTER, DIMENSION(:), POINTER :: char END TYPE STRING TYPE(STRING):: var1, var2 ALLOCATE(var1%char(8)) var1%char=(/"M","y","V","a","l","u","e","s"/)
← Previous Page
← Previous Page