Jump to content United States-English
HP.com Home Products and Services Support and Drivers Solutions How to Buy
» Contact HP
More options
HP.com home
HP Fortran Compiler for HP-UX: HP Fortran Programmer's Reference > Chapter 3 Arrays

Array-valued structure component references

» 

Technical documentation

Complete book in PDF
» Feedback
Content starts here

 » Table of Contents

 » Glossary

 » Index

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”).

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

The next statement uses a subscript triplet in an array-valued structure component reference to assign the same score to one test of three students in one course:

course(2)%student(1:3)%test(4) = 82

It would be convenient if we could initialize all tests of all students in all courses to 0. But the Standard does not allow structure component references in which more than one of the parts specifies a rank greater than 0. In other words, the following is not legal:

course%student%test = 0  ! ILLEGAL

The following example, array_val_ref.f90, contains the code examples listed in this section:

Example 3-4 array_val_ref.f90

PROGRAM main
! illustrates array-valued structure component references

! define a derived type that will be used to declare an
! object of this type as a component of another derived type
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) ! an array of derived
! type
END TYPE course_data

TYPE (course_data) :: course(5) ! an array of derived
! type

! scalar-valued structure component reference
course(5)%student(7)%test(4) = 95
PRINT *, course(5)%student(7)%test(4)

! array-valued structure component reference
course(4)%student%test(3) = 60
PRINT *, course(4)%student%test(3)

! array-valued structure component reference
course(3)%student(3)%test = 0
PRINT *, course(3)%student(3)%test

! array-valued structure component reference, using
! a subscript triplet to reference a section of the
! array component student
course(2)%student(1:3)%test(4) = 82
PRINT *, course(2)%student(1:3)%test(4)

! the following commented-out statement is illegal:
! only one part (of the combined components and
! parent) in a structure component reference
! may have a rank greater than 0.
! course%student%test = 0

END PROGRAM main

Here are the command lines to compile and execute the program, along with the output from a sample run:

$ f90 array_val_ref.f90
$ a.out
95
60 60 60 60 60 60 60 60 60 60
0 0 0 0
82 82 82
Printable version
Privacy statement Using this site means you accept its terms Feedback to webmaster
© Hewlett-Packard Development Company, L.P.