Passing a 3-D array as a 2-D array
Author Message
Passing a 3-D array as a 2-D array

Hi:

I forgot how to do this, and I could not find answers from the
library since the books are very elementary(?)

Here is the problem:

c*****MAIN
c     dimension c(3,6,6)
.....
.....
call func(c(1,1,1)
.....
stop
and
c
c*****SUBROUTINE
c
subroutine func(c)
dimension c(6,6)
....
....
return
end

From the calling part, I want the array returned from func() goes
to c(1,6,6) in the MAIN.  There is a way to do it, but I just forgot it.

Ji Wang
--

/\    /\    / Department  of   Civil   Engineering  & Operations  Research
/  \  /  \  / Princeton University WWW: http://www.*-*-*.com/ :80/~jiwang
__/    \/    \/ Princeton NJ 08544-5263  Tel:(609)258-6239 Fax:(609)258-1270

Sun, 25 Jan 1998 03:00:00 GMT
Passing a 3-D array as a 2-D array

Quote:

>c*****MAIN
>c     dimension c(3,6,6)
>      .....
>      .....
>      call func(c(1,1,1)
>      .....
>      stop
>      and
>c
>c*****SUBROUTINE
>c
>      subroutine func(c)
>      dimension c(6,6)
>      ....
>      ....
>      return
>      end

>From the calling part, I want the array returned from func() goes
>to c(1,6,6) in the MAIN.  There is a way to do it, but I just forgot it.

The only straightforward way to do it is to pass an array section
(which is a fortran 90 feature):

call func(c(1,:,:))

If you are stuck with Fortran 77, the problem is that func expects the
array to occupy consecutive memory locations, while the calling program
has the desired array elements in nonconsecutive locations.  If you
compare the corresponding locations, you find:

locations in main       locations in func
-----------------       -----------------
c(1,1,1)               c(1,1)
c(2,1,1)                 ?
c(3,1,1)                 ?
c(1,2,1)               c(2,1)
c(2,2,1)                 ?
c(3,2,1)                 ?
c(1,3,1)               c(3,1)
...                    ...

Short of copying the elements you want to another array, about the only
other way out of this mess in Fortran 77 is to reverse the order of the
subscripts:

dimension c(6,6,3)
...
call func(c(1,1,1))
...
end

subroutine func(c)
dimension c(6,6)
...
end

This way, the elements that are seen by func are the first 36 elements
of c in the main program, and they are consecutive locations (no
gaps).

Dave Seaman

Mon, 26 Jan 1998 03:00:00 GMT

 Page 1 of 1 [ 2 post ]

Relevant Pages