Fortran中的常见块

时间:2011-10-23 10:19:05

标签: fortran block fortran-common-block

Fortran在公共块中是否有公共块?就像结构中有结构一样。 E.g。

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

上面的代码是否表示common2,在common1?

1 个答案:

答案 0 :(得分:0)

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

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中的点(。)运算符类似。它允许您访问派生类型中包含的数据。

相关问题