Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/fortran/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Fortran 是否禁止在变量声明之间初始化变量?_Fortran - Fatal编程技术网

Fortran 是否禁止在变量声明之间初始化变量?

Fortran 是否禁止在变量声明之间初始化变量?,fortran,Fortran,我有点像 integer a integer b b = 0 integer c a = 0 c = 0 这与错误无关 “规范语句不能出现在可执行文件部分。” 但是,将其更改为 integer a integer b integer c a = 0 b = 0 c = 0 有效。是的,这在Fortran中是禁止的。这在Fortran 2008标准第2.3.2节“语句顺序”中有定义: 1第2.1条的语法规则规定了程序单元和子程序中的语句顺序。这些规则 如表2.1[…]所示。表2.1显

我有点像

integer a
integer b 
b = 0
integer c

a = 0
c = 0
这与错误无关

“规范语句不能出现在可执行文件部分。”

但是,将其更改为

integer a
integer b
integer c

a = 0
b = 0
c = 0 

有效。

是的,这在Fortran中是禁止的。这在Fortran 2008标准第2.3.2节“语句顺序”中有定义:

1第2.1条的语法规则规定了程序单元和子程序中的语句顺序。这些规则 如表2.1[…]所示。表2.1显示了语句的排序规则,适用于 所有程序单元、子程序和接口体。垂直线描绘了各种可能的陈述 横线勾勒出各种各样的陈述,而这些陈述不应该被穿插。[…]在中的USE和CONTAINS语句之间 子程序,不可执行语句通常在可执行语句之前[…]

(强调矿山)


[有点离题,但相关]请注意

integer :: a
integer :: b = 0
integer :: c

是允许的,这有一个副作用,即
b
获取
save
属性。这通常不是您想要的…

是的,这在Fortran中是禁止的。这在Fortran 2008标准第2.3.2节“语句顺序”中有定义:

1第2.1条的语法规则规定了程序单元和子程序中的语句顺序。这些规则 如表2.1[…]所示。表2.1显示了语句的排序规则,适用于 所有程序单元、子程序和接口体。垂直线描绘了各种可能的陈述 横线勾勒出各种各样的陈述,而这些陈述不应该被穿插。[…]在中的USE和CONTAINS语句之间 子程序,不可执行语句通常在可执行语句之前[…]

(强调矿山)


[有点离题,但相关]请注意

integer :: a
integer :: b = 0
integer :: c

是允许的,这有一个副作用,即
b
获取
save
属性。这通常不是您想要的…

错误信息非常清楚。Fortran程序和子程序分为两部分。首先是使用模块、定义变量、派生类型、接口的规范部分。。。然后是可执行部分,在其中放置实际的可执行语句或控制结构


无法将它们混合使用。

错误信息非常清楚。Fortran程序和子程序分为两部分。首先是使用模块、定义变量、派生类型、接口的规范部分。。。然后是可执行部分,在其中放置实际的可执行语句或控制结构


不可能将它们混合在一起。

在f2008中,使用块构造时,情况更为模糊。该构造必然位于可执行语句之间,但其唯一目的通常是在可执行语句之后添加一些规范语句的功能,例如假定长度的指针指向匿名内存

编辑:示例

module anonymous
   use ISO_C_BINDING
   implicit none
   interface
      function malloc(size) bind(C,name='malloc')
         import
         implicit none
         type(C_PTR) malloc
         integer(C_SIZE_T), value :: size
      end function malloc
      subroutine free(ptr) bind(C,name='free')
         import
         implicit none
         type(C_PTR), value :: ptr
      end subroutine free
      function strlen(str) bind(C,name='strlen')
         import
         implicit none
         type(C_PTR), value :: str
         integer(C_SIZE_T) strlen
      end function strlen
   end interface
   contains
      function hello()
         type(C_PTR) hello
         character(LEN=*,KIND=C_CHAR), parameter :: world = &
            'Hello, world'//C_NULL_CHAR
         character(LEN=len(world),KIND=kind(world)), pointer :: fptr
         hello = malloc(len(world,KIND=C_SIZE_T))
         call C_F_POINTER(hello,fptr)
         fptr = world
      end function hello
end module anonymous

program test
   use anonymous
   implicit none
   type(C_PTR) cptr
   character(LEN=:,KIND=C_CHAR), pointer :: fptr
   integer(C_SIZE_T) hello_len
   cptr = hello()
   hello_len = strlen(cptr)
   BLOCK
      character(LEN=hello_len,KIND=C_CHAR), pointer :: temp
      call C_F_POINTER(cptr,temp)
      fptr => temp
   end BLOCK
   write(*,'(*(g0))') fptr(1:strlen(cptr))
end program test

在f2008中,情况更为模糊,采用了区块结构。该构造必然位于可执行语句之间,但其唯一目的通常是在可执行语句之后添加一些规范语句的功能,例如假定长度的指针指向匿名内存

编辑:示例

module anonymous
   use ISO_C_BINDING
   implicit none
   interface
      function malloc(size) bind(C,name='malloc')
         import
         implicit none
         type(C_PTR) malloc
         integer(C_SIZE_T), value :: size
      end function malloc
      subroutine free(ptr) bind(C,name='free')
         import
         implicit none
         type(C_PTR), value :: ptr
      end subroutine free
      function strlen(str) bind(C,name='strlen')
         import
         implicit none
         type(C_PTR), value :: str
         integer(C_SIZE_T) strlen
      end function strlen
   end interface
   contains
      function hello()
         type(C_PTR) hello
         character(LEN=*,KIND=C_CHAR), parameter :: world = &
            'Hello, world'//C_NULL_CHAR
         character(LEN=len(world),KIND=kind(world)), pointer :: fptr
         hello = malloc(len(world,KIND=C_SIZE_T))
         call C_F_POINTER(hello,fptr)
         fptr = world
      end function hello
end module anonymous

program test
   use anonymous
   implicit none
   type(C_PTR) cptr
   character(LEN=:,KIND=C_CHAR), pointer :: fptr
   integer(C_SIZE_T) hello_len
   cptr = hello()
   hello_len = strlen(cptr)
   BLOCK
      character(LEN=hello_len,KIND=C_CHAR), pointer :: temp
      call C_F_POINTER(cptr,temp)
      fptr => temp
   end BLOCK
   write(*,'(*(g0))') fptr(1:strlen(cptr))
end program test

需要注意的是,锁中定义的标识符的范围仅限于块。您所说的假定长度指针到底是什么意思?为什么是匿名目标?叹气。。。“您好,”世界计划补充道,“这实际上是一个非常有趣的用法,尽管这里没有主题。”。也可以作为子例程实现。您想说的是指向C分配的内存的延迟长度字符指针吗?IMHO会更清楚,可能不需要长示例。这样一个主要解决如何将Fortran延迟长度字符串指向C缓冲区的长示例(IMHO)是离题的。仅仅用指针来说明这个块,也许只有五分之一的行就足够了。老实说,我认为OP是一个初学者,可能在您的高级示例中丢失了。我承认我要求了这个例子,但我真的不明白你在答案的第一个版本中说了什么(现在仍然存在)。有一点需要注意,锁中定义的标识符的范围仅限于块。你所说的假定长度指针到底是什么意思?为什么是匿名目标?叹气。。。“您好,”世界计划补充道,“这实际上是一个非常有趣的用法,尽管这里没有主题。”。也可以作为子例程实现。您想说的是指向C分配的内存的延迟长度字符指针吗?IMHO会更清楚,可能不需要长示例。这样一个主要解决如何将Fortran延迟长度字符串指向C缓冲区的长示例(IMHO)是离题的。仅仅用指针来说明这个块,也许只有五分之一的行就足够了。老实说,我认为OP是一个初学者,可能在您的高级示例中丢失了。我承认我要求举个例子,但我真的不明白你在第一个版本的答案中说了什么(现在仍然在那里)。我想保持简单,因为这是一个初级水平的问题。我想保持简单,因为这是一个初级水平的问题。