How can a scalar be passed to a vector (1D array) to a Fortran subroutine?

后端 未结 2 1893
梦谈多话
梦谈多话 2020-11-27 23:08

There is this program:

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==================         


        
2条回答
  •  一整个雨季
    2020-11-27 23:52

    EDIT: corrected per the comment by IanH, who points out that the behavior is guaranteed without making assumptions about the argument passing convention.

    This approach started in early FORTRAN, by assuming that the argument is being passed as an address, typically called "call by reference". The address of scaler mp(1,k) is the address of the first element of this column k. Since Fortran stores arrays in column major format (http://en.wikipedia.org/wiki/Row-major_order#Column-major_order), the 15 values of the kth column will be sequential in memory. So if the called subroutine interprets this address as that of a 1-D array tensB of length 15, it will access the elements of the kth column.

    In modern Fortran one could write the argument in a clearer manner by selecting a column with an array slice: mp (:,k).

提交回复
热议问题