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.

A pointer example<br />

Data types and data objects<br />

Pointers<br />

The example below, ptr_sts.f90, illustrates different pointer operations, including calls to the<br />

ASSOCIATED intrinsic to determine pointer status.<br />

Example 3-4 ptr_sts.f90<br />

PROGRAM main<br />

! This program performs simple pointer operations, including<br />

! calls to the ASSOCIATED intrinsic to determine status.<br />

!<br />

! Declare pointer as a deferred shape array with POINTER<br />

! attribute.<br />

REAL, POINTER :: ptr(:)<br />

REAL, TARGET :: tgt(2) = (/ -2.2, -1.1 /) ! initialize target<br />

PRINT *, "Initial status of pointer:"<br />

call get_ptr_sts<br />

ptr => tgt ! pointer assignment<br />

PRINT *, "Status after pointer assignment:"<br />

call get_ptr_sts<br />

PRINT *, "Contents of target by reference to pointer:", ptr<br />

! use an array constructor to assign to tgt by reference to ptr<br />

ptr = (/ 1.1, 2.2 /)<br />

PRINT *, “Contents of target after assignment to pointer:”, tgt<br />

NULLIFY(ptr)<br />

PRINT *, "Status after pointer is nullified:"<br />

call get_ptr_sts<br />

ALLOCATE(ptr(5)) ! allocate pointer<br />

PRINT *, "Status after pointer is allocated:"<br />

! To learn if pointer is allocated, call the ASSOCIATED<br />

! intrinsic without the second argument<br />

IF (ASSOCIATED(ptr)) PRINT *, " Pointer is allocated."<br />

ptr = (/ 3.3, 4.4, 5.5, 6.6, 7.7 /) ! array assignment<br />

PRINT *, ‘Contents of array pointer:’, ptr<br />

DEALLOCATE(ptr)<br />

PRINT *, “Status after array pointer is deallocated:"<br />

IF (.NOT. ASSOCIATED(ptr)) PRINT *, " Pointer is deallocated."<br />

CONTAINS<br />

! Internal subroutine to test pointer’s association status.<br />

! Pointers can be passed to a procedure only if its interface<br />

Chapter 3 51

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

Saved successfully!

Ooh no, something went wrong!