Fortran语言:在主程序中的子程序通过&QUOT传递一个标量为向量;调用"命令? [英] Fortran: pass a scalar in a main routine to a vector in a subroutine via "CALL" command?

查看:247
本文介绍了Fortran语言:在主程序中的子程序通过&QUOT传递一个标量为向量;调用"命令?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有是这样的程序:

INTEGER i,k
REAL*8  mp(15,48)
REAL*8  sp(15)
k=0
do i=1,12
   k=k+1
   call Equaltensors(sp,mp(1,k),15)
enddo
end

c=====================

subroutine Equaltensors(tensA,tensB,n)
REAL*8 tensA(n),tensB(n)
INTEGER i
do   i=1,n
     tensB(i)=tensA(i)
enddo
return
end

所以熔点(1,1)等的基本值被传递给子程序作为载体tensB(15)中n = 15。我不明白的是一个真实的数字如何被存储在一个子程序的一维数组。

So basically the value of mp(1,1) and so on is passed to the subroutine as a vector tensB(15) with n=15. What I don't understand is how a real number can be stored in a one-dimension array in a subroutine.

任何帮助将是AP preciated。

Any help would be appreciated.

推荐答案

你的问题的标题有点误导。 Fortran不允许你到一个标量传递给数组。但是,它允许在传递一个数组的一个元素例行的数组伪参数 - 这就是所谓的FORTRAN序列协会。由于IanH和其他人说,以下内容会自动与虚拟数组的元素相关联,到调用程序的实际数组中的最后一个元素。

The title of your question is a bit misleading. Fortran doesn't allow you to pass a scalar to an array. But what it DOES allow is passing a single element of an array to a routine's array dummy argument - this is called "sequence association" in Fortran. As IanH and others have said, the following elements are automatically associated with the elements of the dummy array, up to the last element in the called routine's actual array.

有此功能的一些限制,但。如果元素是一个指针数组,你不能做到这一点。

There are some restrictions on this feature, though. If the element is of a POINTER array,you can't do this.

让我们回到你的标题,我见过很多程序通过,也就是说,恒3例行那里的假是一个数组。该程序只使用第一个元素,但是这是不合法的和新的编译器可以检测到错误并抱怨。这其中的一个解决办法是使用数组构造把参数到一个数组中 - 例如,调用foo([3]),但是这只有当该值将被读取,不写

Going back to your title, I have seen many programs pass, say, the constant 3 to a routine where the dummy is an array. The routine only uses the first element, but this is not legal and newer compilers may detect the error and complain. One workaround for this is to turn the argument into an array by using an array constructor - for example, CALL FOO ([3]), but this works only if the value is to be read, not written.

我已经写了这个一般性问题的一些博客文章 - 见<一href=\"http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument\" rel=\"nofollow\">http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument和<一个href=\"http://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2\" rel=\"nofollow\">http://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2

I've written some blog posts on this general issue - see http://software.intel.com/en-us/blogs/2009/03/31/doctor-fortran-in-ive-come-here-for-an-argument and http://software.intel.com/en-us/blogs/2009/07/10/doctor-fortran-in-ive-come-here-for-an-argument-side-2

这篇关于Fortran语言:在主程序中的子程序通过&QUOT传递一个标量为向量;调用&QUOT;命令?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆