Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
34 Views

Passing negative indexed arrays to a subroutine

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
Highlighted
34 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
Highlighted
34 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