02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Pointers<br />

Data types and data objects<br />

Pointers<br />

Pointers in <strong>Fortran</strong> 90 are more strongly typed than in other languages. While it is true that<br />

the <strong>Fortran</strong> 90 pointer holds the address of another variable (the target), it also holds<br />

additional information about the target. For this reason, declaring a pointer requires not only<br />

the POINTER attribute but also the type, kind parameter, and (if its target is an array) rank of<br />

thetargetitcanpointto.<br />

If a pointer is declared as an array with the POINTER attribute, it is an array pointer. As<br />

explained in “Deferred-shape arrays” on page 61, the declaration for an array pointer specifies<br />

its specifies rank but not the bounds. Following is the declaration of the array pointer ptr:<br />

REAL(KIND=16), POINTER, DIMENSION(:,:) :: ptr<br />

To become assignable to an array pointer, a target must be declared with the TARGET attribute<br />

and must have the same type, kind parameter, and rank as the array pointer. Given the<br />

previous declaration of ptr, the following are legal statements:<br />

! declare a target with the same type, kind parameter, and<br />

! rank as ptr<br />

REAL(KIND=16), TARGET, DIMENSION(4,3) :: x<br />

...<br />

ptr => x ! assign x to ptr in a pointer assignment statement<br />

Once the assignment statement executes, you can use either ptr or x to access the same<br />

storage, effectively making ptr an alias of x.<br />

You can also allocate storage to a pointer by means of the ALLOCATE statement. To deallocate<br />

that storage after you are finished with it, use the DEALLOCATE statement. Although allocating<br />

storage to a pointer does not involve a target object, the declaration of the pointer must still<br />

specify its type, kind parameter, and (if you want to allocate an array) rank. The ALLOCATE<br />

statement specifies the bounds for the dimensions. Here is an example of the ALLOCATE<br />

statement used to allocate storage for ptr:<br />

INTEGER :: j = 10, k = 20<br />

...<br />

! allocate storage for ptr<br />

ALLOCATE (ptr(j,k))<br />

ptr can now be referenced as though it were an array, using <strong>Fortran</strong> 90 array notation.<br />

As an extension, <strong>HP</strong> <strong>Fortran</strong> provides the Cray-style pointer variables; for more information,<br />

see Chapter 10. For information about aspects of pointers, refer to:<br />

“Array pointers” on page 61 for information about allocating array pointers.<br />

Chapter 3 49

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!