Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28867 Discussions

Passing negative indexed arrays to a subroutine

Navdeep_Rana
Beginner
1,661 Views

Say I define and allocate array

integer, parameter :: n = 128
real,dimension(:) :: u
allocate(u(-1:n+2))

Now I would I like to pass it to a subroutine,

subroutine do_something(u)

real,dimension(:),intent(inout) :: u

u(-1) = 0

end subroutine do_something

This ends up with a warning. How do I let the subroutine know that the array has negative indices?

0 Kudos
2 Replies
jimdempseyatthecove
Honored Contributor III
1,661 Views

Look at Assumed-Shape Specifications in the documentation.

If your array size is unknown, but the array base index is always -1, then

subroutine do_something(u)
  real,dimension(-1:),intent(inout) :: u
  u(-1) = 0
end subroutine do_something

** keep in mind the note in the documentation that assumed shape array passing requires an interface for the subroutine/function (if not within a module).

Jim Dempsey

0 Kudos
jimdempseyatthecove
Honored Contributor III
1,661 Views

Or Assumed-Rank specification:

subroutine do_something(u)
  real,dimension(..),intent(inout) :: u
  if(RANK(u) .ne.1) Stop ! 1D array only
  print *,lbound(u)
  u(-1) = 0
end subroutine do_something

Jim Dempsey

0 Kudos
Reply