Search code examples
fortrangfortranfortran77

How to Interchange dummy variables in Fortran 77?


Problem: I have an array which is created in the middle of a computation. I wanted to take back the information in this array to the main program and print it.

For example:

 PROGRAM DRIVER
  CHARACTER A(*)*(*),B(*)*(*)

  CALL TEST1(A,B)

  write(*,*) 'Print B  ', B


  END

  SUBROUTINE TEST1(A,B)
  CHARACTER A(*)*(*),B(*)*(*)

  A(1)='APPLE'
  A(2)='BAT'

  B(1:2)=A(1:2)


  END

In the above program, the array A is created in "SUBROUTINE TEST1" and I do not know the length of A. I wanted to bring back the information in 'A' through 'B' and print.

When I compile the above program, I am getting the following error.

gfortran -c -O4 -mcmodel=medium -fno-automatic -std=legacy -z muldefs -ff2c -frepack-arrays -fall-intrinsics   Test.f
Test.f:2.24:

      CHARACTER A(*)*(*),B(*)*(*)                                       
                        1
Error: Assumed size array at (1) must be a dummy argument
Test.f:2.33:

      CHARACTER A(*)*(*),B(*)*(*)                                       
                                 1
Error: Assumed size array at (1) must be a dummy argument
Test.f:6.29:

      write(*,*) 'Print B  ', B                                         
                             1
Error: The upper bound in the last dimension must appear in the reference to the assumed size array 'b' at (1)
Test.f:6.72:

      write(*,*) 'Print B  ', B                                         
                                                                        1
Error: Data transfer element at (1) cannot be a full reference to an assumed-size array
make: *** [Test.o] Error 1

Solution

  • In C/C++ and Fortran you can pass `assumed size' arrays in and out of functions/subroutines. This is because arrays contain an address for a block of memory. The program then calculates a memory address for an element in this array using the starting address and the array index values. If you have a multi-dimensional array, you need to know how big the steps have to be for all but one of the dimensions. Imagine you have a 2D array. If you know that the x axis is 20 elements long, then y can be an undefined size, but you can always calculate a memory address for an (x,y) position. Address = 20 * y + x. As a general rule the slowest changing index is the one that can be an undefined size. In my example that's the y axis. In your code you're using arrays of character arrays. That means you've got 2 undefined dimension sizes. That's what the compiler error messages are referring to. If you define the size of the character arrays you should be able to get your code to compile.