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.

A type declaration statement<br />

Another nested structure<br />

A nested record<br />

A union definition<br />

Note that a structure definition allows multiple levels of nesting.<br />

<strong>HP</strong> <strong>Fortran</strong> statements<br />

STRUCTURE (extension)<br />

For programmers who are familiar with C or Pascal, <strong>HP</strong> <strong>Fortran</strong> unions are similar to unions<br />

in C and variant records in Pascal. <strong>HP</strong> <strong>Fortran</strong> unions differ from C unions in that they must<br />

be defined inside a structure definition.<br />

The structure below contains a union with two map blocks. The first contains the integer field<br />

int; the second contains the real field float.<br />

STRUCTURE /var/<br />

INTEGER :: type ! 1=INTEGER, 2=REAL<br />

UNION<br />

MAP<br />

INTEGER :: int<br />

END MAP<br />

MAP<br />

REAL :: float<br />

END MAP<br />

END UNION<br />

END STRUCTURE<br />

To declare a record of this structure named v, use the following RECORD statement:<br />

RECORD /var/ v<br />

The declaration of the record v reserves 8 bytes of storage: 4 bytes for the type field and 4<br />

bytes to be shared by int and float. Ifyouusetheint field to access the 4 bytes, they will be<br />

interpreted as an integer; if you use the float field, they will be interpreted as a real.<br />

It is the programmer’s responsibility to ensure that appropriate values are assigned to each<br />

field in a union. For instance, given the previous declaration of v, the following assignments<br />

make sense:<br />

v.type =1 ! set the type to integer<br />

! access the storage shared by 'int' and 'float' as an integer<br />

v.int = 3<br />

In contrast, the following code would yield unexpected results, although it would compile<br />

without errors:<br />

v.type = 1 ! set the type to integer<br />

! the next statement contradicts the previous statement<br />

v.float = 3.14<br />

Chapter 10 437

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

Saved successfully!

Ooh no, something went wrong!