Fortran中的公共块

Fortran中的公共块,fortran,block,fortran-common-block,Fortran,Block,Fortran Common Block,Fortran在公共块中有公共块吗?就像结构中有结构一样。例如 integer int 1, int2 common/Common1/int1, int2 float f1, f2 common/Common2/f1, f2 save/Common2/, /Common1/ 上述代码是否表示common1中的common2?否,您编写的代码无效。公共块只是一个命名的内存区域 Fortran派生的数据类型与C结构非常相似。Fortran派生的类型声明如下所示: type float_struct

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

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

上述代码是否表示common1中的common2?

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

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

type float_struct
  real :: f1, f2
end type
subroutine sub()
use my_structs_mod
type (my_struct) :: ms
ms%int1 = 42
...
end subroutine
现在,您可以声明另一个包含此类型变量的派生类型:

type my_struct
  integer :: int1, int2
  type (float_struct) :: my_float_struct
end type
请注意,这些是类型的声明,而不是该类型变量的实例化。最好将声明放在模块中,允许您在子例程、函数或程序中访问它们。例如,假设上面的声明放在名为my_structs_mod的模块中。然后您可以像这样使用它们:

type float_struct
  real :: f1, f2
end type
subroutine sub()
use my_structs_mod
type (my_struct) :: ms
ms%int1 = 42
...
end subroutine
请注意,百分号%与点相似。运算符。它允许您访问派生类型中包含的数据