passing pointer arguments in fortran

前端 未结 2 1703
误落风尘
误落风尘 2021-01-21 22:00

I am wondering what is the proper way to write the following code?

PROGRAM foo

  INTEGER :: x
  REAL(KIND=8), TARGET, DIMENSION(0: 10) :: array
  REAL(KIND=8),          


        
2条回答
  •  清酒与你
    2021-01-21 23:06

    It depends what you mean with "proper" way. As IanH already pointed out, you need an explicit interface (best done via packing things in a module) and using allocatables instead of pointers, if possible.

    I'd further add, that if you do not want to change the allocation status of your array within your subroutine, but only want to manipulate its elements, then use a simple assumed shape array within your subroutine. Below you find a working example for it. A few more things to note:

    • Do not use real(kind=8) as not all compilers use the number of bytes as kind for the real numbers. If you want double precision accuracy, ask for it explicitely as below.

    • If you just want to fill up an array with a constant value, do it the simple way: array(:) = 2.0_dp

    And here the example:

    module accuracy
      implicit none
    
      integer, parameter :: dp = kind(1.0d0)
    
    end module accuracy
    
    
    module barmodule
      use accuracy
      implicit none
    
    contains
    
      subroutine  bar(array)
        real(dp), intent(inout) :: array(:)
    
        integer :: ii
    
        do ii = 1, size(array)
          array(ii) = ii
        end do
    
      end subroutine bar
    
    end module barmodule
    
    
    program foo
      use accuracy
      use barmodule
      implicit none
    
      real(dp), dimension(0:10) :: array
      call bar(array)
    
    end program foo
    

提交回复
热议问题