HP Fortran Programmer's Reference (September 2007)

Arrays
Array-valued structure component references
Chapter 372
Array-valued structure component references
A structure component reference can specify an array or a scalar. If, for example, the parent in
the reference is declared as an array and likewise one of the components is declared as an
array, this makes possible an array-valued structure component reference. Conceptually, an
array-valued structure component reference is similar to a reference to an array section (see
Array sections” on page 68).
Consider the following code:
TYPE student_data
CHARACTER(25) :: name
INTEGER :: average, test(4)
END TYPE student_data
TYPE course_data
CHARACTER(25) :: course_title
INTEGER :: course_num, class_size
TYPE(student_data) :: student(10)
END TYPE course_data
TYPE (course_data) :: course(5)
These statements prepare a database for maintaining course information for 50 students—10
students per course. The information about the students is held in student—an array of
derived type. Likewise, the information about the five courses is held in course, which is also
an array of derived type and which has student as one of its components. The following
statement assigns a test score to a one student in one course, using a structure component
reference:
course(5)%student(7)%test(4) = 95
The reference is scalar-valued: 95 is assigned to a single element, test(4) of student(7) of
course(5).
However, it is also possible to reference more than one element in a structure component
reference. The following statement assigns the same score to one test taken by all students in
one course:
course(4)%student%test(3) = 60
The structure component reference is array-valued because thirty elements are assigned with
the one reference. The reference is to a section of the array course, rather than to the entire
array.
The next statement also makes an array-valued structure component reference to initialize
all the tests of one student in one course:
course(3)%student(3)%test = 0