Skip to main content
Engineering LibreTexts

16.3: Accessing Components

  • Page ID
    54368
  • \( \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}}\)

    Once some variables using the derived data type have been declared, the individual components can be accessed. To access a component of a derived data type, specify the variable name followed by a “%” (percent sign) followed by the component name. The general format is:

    <variable_name>%<component_name>
    

    For example, to set all components for the student student1, the following

    student1%name = "Joseph"
    student1%id = 1234
    student1%score = 99.99
    student1%grade = "A"
    

    Each component for student1 is set individually. Not every component must be set. Of course, as with other variables, any component that has not been set cannot be used.

    This previous declaration and these assignments will establish a variable as follows:

    The student1 declaration.

    It is possible to assign all components to another variable of the same derived data type. For example, to set student2 to be the same as student1, an assignment is used as follows:

    student2 = student1
    

    This will copy all components from the variable student1 into the variable student2 (since both student1 and student2 are of the same derived data type).


    This page titled 16.3: Accessing Components 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?