声明数组的不同语法:使用和不使用维度语句 [英] Different syntaxes to declare arrays: with and without the dimension statement
问题描述
我正在使用gfortran
版本7.2.0.我是Fortran的新手.我知道有不同版本的Fortran.在下面的代码中,我使用不同的语法声明数组(或实际上是张量)
I'm using gfortran
version 7.2.0. I'm quite new to Fortran. I know there are different versions of Fortran. In the code below, I'm declaring arrays (or actually tensors) using different syntaxes
program arrays
implicit none
integer :: m(3, 4)
integer, dimension(3, 4) :: n
print *, "m = ", m
print *, "n = ", n
end program arrays
在一种情况下,我使用的是dimension
语句,在另一种情况下,我没有使用.该程序进行编译(没有错误).我正在使用gfortran
的标志-g
和-fbounds-check
.使用上述程序的文件的文件扩展名是f.90
.
In one case, I'm using the dimension
statement, in the other I am not. This program compiles (without errors). I'm using the gfortran
's flags -g
and -fbounds-check
. The file extension of the file with the program above is f.90
.
为什么在Fortran中显然有不同的语法来声明数组?哪些版本的Fortran支持哪种语法,或者可以声明数组的 rank , shapes 和 extents ,就像m
一样编译器的扩展名?
Why are there different syntaxes to apparently declare arrays in Fortran? Which versions of Fortran support which syntaxes, or is the possibility to declare the rank, shapes and extents of arrays as for m
just an extension of the compiler?
推荐答案
语句
integer :: m(3, 4)
integer, dimension(3, 4) :: n
自从Fortran 90以来,
都是标准的Fortran.不使用::
的第一行就是
are both standard Fortran since Fortran 90. Without the use of the ::
the first line like
integer m(3,4)
在Fortran 90之前有效.
would be valid before Fortran 90.
在介绍其他内容之前,,dimension
不是维度声明,而是属性说明.尺寸说明应为
Before coming to something else, the ,dimension
isn't a dimension statement but an attribute specification. A dimension statement would be
dimension n(3,4) ! With n implicitly or explicitly typed elsewhere
这里重要的是,用类型声明指定的属性适用于(几乎)所有所有对象.所以
The important thing here is that attributes specified with type declaration apply to (almost) all objects declared. So
integer :: m1(3,4), m2, m3
integer, dimension(3,4) :: n1, n2, n3
看到m1
一个等级为2的数组,但是m2
和m3
标量(除非在其他位置给出了数组属性或实际上是函数),而n1
,n2
和n3
都为等级2形状为[3,4]
sees m1
a rank-2 array, but m2
and m3
scalars (unless given array properties elsewhere or are actually functions) whereas n1
, n2
and n3
are all rank-2 arrays of shape [3,4]
问题的两个声明可以简单地
The two declarations of the question could then be simply
integer, dimension(3,4) :: m, n
几乎"来自我们可以拥有的事实
The "almost" comes from the fact that we can have
integer, dimension(3,4) :: n, p(5)
其中p
的形状为[5]
,覆盖了先前指定的[3,4]
.
where the shape of p
is [5]
, overriding the [3,4]
specified earlier.
这篇关于声明数组的不同语法:使用和不使用维度语句的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!