【问题标题】:Common Blocks in FortranFortran 中的常用块
【发布时间】:2011-10-23 10:19:04
【问题描述】:

Fortran 在公共块中有公共块吗?就像结构中有结构一样。例如

integer int 1, int2
common/Common1/int1, int2
float f1, f2
common/Common2/f1, f2
save/Common2/, /Common1/

上面的代码是不是表示common2, in within common1?

【问题讨论】:

    标签: fortran block fortran-common-block


    【解决方案1】:

    不,您编写的代码无效。公共块只是一个命名的内存区域。

    Fortran 具有与 C 结构非常相似的“派生数据类型”。 Fortran 派生类型声明如下所示:

    type float_struct
      real :: f1, f2
    end type
    

    现在您可以声明另一个包含此类型变量的派生类型:

    type my_struct
      integer :: int1, int2
      type (float_struct) :: my_float_struct
    end type
    

    请注意,这些是一种类型的声明,而不是该类型变量的实例化。最好将声明放在模块中,以便您在子例程、函数或程序中访问它们。例如,假设上面的声明放在名为“my_structs_mod”的模块中。然后你可以像这样使用它们:

    subroutine sub()
    use my_structs_mod
    type (my_struct) :: ms
    ms%int1 = 42
    ...
    end subroutine
    

    请注意,百分号 (%) 类似于 C 中的点 (.) 运算符。它允许您访问派生类型中包含的数据。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2013-06-27
      • 1970-01-01
      • 2012-04-19
      • 2015-11-19
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多