- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
Hello!
I write a subroutine setrx and setix expecting a multi-dimensional array.
When they are called in main code, an address of a location in a one dimensional array
can be passed to them. It works well.
But After I put the setrx and setix in a interface, calling the setx with the address
of one-dimension array will lead compile errors.
I need the help. Thanks in advance.
!#########################################################
program main
!#########################################################
implicit none
interface setx
subroutine setrx(m,n,x)
integer,intent(in) :: m
integer,intent(in) :: n
real,intent(out) :: x(m,*)
end subroutine setrx
subroutine setix(m,n,x)
integer,intent(in) :: m
integer,intent(in) :: n
integer,intent(out):: x(m,*)
end subroutine setix
end interface setx
real :: rx(10),rxx(3,10)
integer :: ix(10),ixx(4,10)
call setrx(1,10,rx)
call setix(1,10,ix)
call setrx(3,10,rxx)
call setix(4,10,ixx)
!
! the following calls will lead two errors
!
! error #6285: There is no matching specific subroutine for this generic subroutine call. [SETX]
! ===================================================
call setx(1,10,rx)
call setx(1,10,ix)
! ===================================================
!
! the following calls are ok
!
call setx(3,10,rxx)
call setx(4,10,ixx)
!
end
end program main
!=====================================================
subroutine setrx(m,n,x)
!=====================================================
implicit none
integer,intent(in) :: m
integer,intent(in) :: n
real,intent(out) :: x(m,*)
integer :: i,j
do j=1,n
do i=1,m
x(i,j) = 1.0*i*j
end do
end do
end subroutine setrx
!=====================================================
subroutine setix(m,n,x)
!=====================================================
implicit none
integer,intent(in) :: m
integer,intent(in) :: n
integer,intent(out):: x(m,*)
integer :: i,j
do j=1,n
do i=1,m
x(i,j) = i*j
end do
end do
end subroutine setix
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
There is another important point at play here than passing a 1-D array or scalar in place of a 2-D dummy argument: overlaid module procedures are called using their generic name. For this reason, the compiler must find a specific subroutine by matching arguments, and it finds none that takes a 1-D array argument.
One way of making the code acceptable to the compiler is to add specific subroutines, say setix1d and setrx1d, that take a 1-D array, of the appropriate type, as the first argument.
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The point of interfaces is to detect such a situation so you can be happy in the knowledge that the compiler is doing the right thing.
If you put your 'set' subroutines in a module you could create specific versions for 1 and 2 d arrays and use module procedure to define a generic interface (as suggested above).
In this particular case you might want to consider doing the array initialization directly:
[fortran]
x = reshape([((i*j,i=1,m),j=1,n)],[m,n])
[/fortran]
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
The purpose of code is cast a address of one-dimension array into multi-dimension array in subroutine.
In subroutines the multi-dimension array will be readable and easy to be manipulated.
For example;
I define a large one-dimension array, F(1000). I wan to use F(200:499) as a three-dimension coordinates array.
So I'd like to cast the F(200) into a multi-dimension array of x[3,100] when subroutines setx is called.
call setx(3,100,F(200))
In subroutines setx, the X(3,100) will be readable.

- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page