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

passing an array of shape (0:N+2) to a function

abaraldi
Beginner
1,352 Views
Hi,
I have the following problem. I have a 3D array:

V(N+2,0:N+2,N+2)

so the second dimension is one unit larger then the other two, and also the second dimension index starts at 0.
Now I pass a portion of it to a subroutine 'sub1'

do i=1:N+2
do k=1:N+2
call sub1(V(i,:,k))
end do
endo do

subroutine sub1(a)

real, dimension(:),intent(in) :: a

...

end subroutine sub1

The problem is that subroutine sub1 thinks that the index of array 'a' goes from 1 to N+3, therefore everything is shifted and it' s a mess! How can I go around it? So that also inside sub1 I can work with index 0 to N+2 for array a.
Thanks
0 Kudos
2 Replies
abaraldi
Beginner
1,352 Views
I found this:

To address this problem, when the lower bound is not 1, one can pass it from the caller and the callee uses it to declare an assumed-shape array.

Suppose the caller has the following:

INTEGER, DIMENSION(-3:3) :: x
   ..........
CALL  SomeOne(x, -3)
The callee could declare an assumed-shape array like the following:
SUBROUTINE  SomeOne(y, LowerBound)
   IMPLICIT  NONE
   INTEGER, DIMENSION(LowerBound:), INTENT(IN) :: y
      ..........
END SUBROUTINE  SomeOne
In this case, the extent of array y() agrees with that of actual argument array x()

If someone has got a better idea that is welcome too! Thanks
0 Kudos
mecej4
Honored Contributor III
1,352 Views
The conventions for passing assumed shape arrays as arguments are defined by the Fortran standard. The default value of the lower bound of an array index is 1.

If you know that the subroutine will always be called with the argument being an array with index starting at 0, you can use the following:

[fortran]SUBROUTINE  SomeOne(y)
   IMPLICIT  NONE
   INTEGER, DIMENSION(0:), INTENT(IN) :: y
      ..........
[/fortran]
0 Kudos
Reply