02.07.2013 Views

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

HP Fortran Programmer's Reference

SHOW MORE
SHOW LESS

You also want an ePaper? Increase the reach of your titles

YUMPU automatically turns print PDFs into web optimized ePapers that Google loves.

Expressions and assignment<br />

Assignment<br />

If target-expression is a pointer already associated with a target, then pointer-object<br />

becomes associated with the target of target-expression. Iftarget-expression is a<br />

pointer that is disassociated or undefined, then pointer-object inherits the disassociated or<br />

undefined status of target-expression. For information about pointer status, see “Pointer<br />

association status” on page 50.<br />

The following example, ptr_assign.f90, illustrates association of scalar and array pointers<br />

with scalar and array targets:<br />

Example 5-1 ptr_assign.f90<br />

PROGRAM main<br />

INTEGER, POINTER :: p1, p2, p3(:) ! declare three pointers, p3<br />

! is a deferred-shape array<br />

INTEGER, TARGET :: t1 = 99, t2(5) = (/ 1, 2, 3, 4, 5 /)<br />

98<br />

! p1, p2 and p3 are currently undefined.<br />

p1 => t1 ! p1 is associated with t1.<br />

PRINT *, 'contents of t1 referenced through p1:', p1<br />

p2 => p1 ! p2 is associated with t1.<br />

! p1 remains associated with t1.<br />

PRINT *, 'contents of t1 referenced through p1 through p2:', p2<br />

p1 => t2(1) ! p1 is associated with t2(1).<br />

! p2 remains associated with t1.<br />

PRINT *, 'contents of t2(1) referenced through p1:', p1<br />

p3 => t2 ! p3 is associated with t2.<br />

PRINT *, &<br />

'contents of t2 referenced through the array pointer p3:', p3<br />

p1 => p3(2) ! p1 is associated with t2(2).<br />

PRINT *, &<br />

'contents of t2(2) referenced through p3 through p1:', p1<br />

NULLIFY(p1) ! p1 is disassociated.<br />

IF (.NOT. ASSOCIATED(p1)) PRINT *, "p1 is disassociated."<br />

p2 => p1 ! Now p2 is also disassociated.<br />

IF (.NOT. ASSOCIATED(p2)) PRINT *, &<br />

"p2 is disassociated by pointer assignment."<br />

END PROGRAM main<br />

Here are the command lines to compile and execute the program, along with the output from<br />

asamplerun:<br />

$ f90 ptr_assign.f90<br />

$ a.out<br />

contents of t1 referenced through p1: 99<br />

Chapter 5

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

Saved successfully!

Ooh no, something went wrong!