Skip to main content
Engineering LibreTexts

16.5: Arrays of Derived Data

  • Page ID
    54370
  • \( \newcommand{\vecs}[1]{\overset { \scriptstyle \rightharpoonup} {\mathbf{#1}} } \) \( \newcommand{\vecd}[1]{\overset{-\!-\!\rightharpoonup}{\vphantom{a}\smash {#1}}} \)\(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\) \(\newcommand{\id}{\mathrm{id}}\) \( \newcommand{\Span}{\mathrm{span}}\) \( \newcommand{\kernel}{\mathrm{null}\,}\) \( \newcommand{\range}{\mathrm{range}\,}\) \( \newcommand{\RealPart}{\mathrm{Re}}\) \( \newcommand{\ImaginaryPart}{\mathrm{Im}}\) \( \newcommand{\Argument}{\mathrm{Arg}}\) \( \newcommand{\norm}[1]{\| #1 \|}\) \( \newcommand{\inner}[2]{\langle #1, #2 \rangle}\) \( \newcommand{\Span}{\mathrm{span}}\)\(\newcommand{\AA}{\unicode[.8,0]{x212B}}\)

    In addition to declaring single variables based on the derived data type definition, it is possible to declare an array based on the derived data type definition. For example, to declare an array named class to hold 30 elements of type student, the following declaration can be used:

    type(student), dimension(30) :: class
    

    Each element of the array class will be of the type student and include each of the defined components (name, id, score, grade in this example). For an array of type(student), the layout would be as follows:

    Array of derived data.

    To access elements in the array, an index must be used. After the index, the desired component would be specified. For example, to set values for the third student, the following statements could be used.

    class(3)%name = "Fred"
    class(3)%id = 4321
    class(3)%score = 75.75
    class(3)%grade = "C"
    

    As with any array, the index can be an integer variable.

    As with single variables, it is possible to assign all components of an array element to another array element, or another variable of the same derived data type. The following declarations and code could be used to swap the location of the fifth student and the eleventh student.

    type student
        character(50) :: name
        integer :: id
        real :: score
        character(1) :: grade
    end type student
    
    type(student), dimension(30) :: class
    type(student) :: temp
    
    temp = class(5)
    class(5) = class(11)
    class(11) = temp
    

    This code fragment will copy all components from the fifth array element (of type student) into a temporary variable (also of type student). Then, the eleventh array element can be copied into the fifth array element (thus overwriting all previous values). And, finally, the eleventh array element can be set to the original values from the fifth array element, which are held in the temporary variable.


    This page titled 16.5: Arrays of Derived Data is shared under a CC BY-NC-SA 3.0 license and was authored, remixed, and/or curated by Ed Jorgensen via source content that was edited to the style and standards of the LibreTexts platform; a detailed edit history is available upon request.

    • Was this article helpful?