[comp.os.vms] Passing Different Variable Types In Fortran, Another Solution...

CLAYTON@xrt.upenn.EDU ("Paul D. Clayton") (05/26/87)

Information From TSO Financial - The Saga Continues...
Chapter 3 - May 22, 1987

The original problem from Richard Steinberger was:

>I have a question concerning a problem that Fortran does not handle well.
>I would like to write a subroutine that gets as arguments the following:
>            (1)  An array
>            (2)  The type of array (byte, integer*2, integer*4, real*4, etc.)
>In the subroutine I want to write to a file (or otherwise manipulate) data
>from the passed array.  Fortran has an obvious problem with data typing.

One of the answers, from Olafur Gudmundsson was:

>>The simple solution to your problem is to use equivalence statements
>>this way there is no overhead because of copying.
>>The only overhead is that you will have to use IF statments to pick the
>>right data to work with
>>Example :
>>	subroutine foo( arr, typ)
>>	int arr( *)   , typ 
>>	real r4( 1)
>>	integer*2 i2(1)
>>	byte	c1(1)
>>	eqivalence (arr(1), r4(1) , i2(1) ,  c1(1))
>>c    and in the subroutine you do
>>	if( typ.eq. INT) then
>>	else if( typ .eq. REA4) then
>>	...
>>	endif
>>	and so on 

The last version of the VAX Fortran compiler I used would not provide for
Mr. Gudmundsson's program due it not allowing equivalence statements to 
reference 'passed arguments'. It appeared at the time, the compiler REQUIRED
a 'known address' at compile time with which to setup the proper machine code.

I suggest a different tack from the one above and it uses the STRUCTURE 
statement that is rather new to VAX Fortran. The record structure could be
placed in a INCLUDE file which would allow for easy access by many subroutines
without typing errors as well as flexability. The comments tell the story line.

	Program test
	structure /data_arrays/
	  integer*2  array_type		!flag variable to show what type of
					! data is following.
	  union				!following are the various data types
	    map				!  and the arrays associated with
	      integer*2	i_by_2(200)	!  each type. They all occupy the same 
	    end map			!  space and thereby CONNOT be used
	    map				!  concurrently.
	      integer*4	i_by_4(200)
	    end map
	    map
	      character*70 char_by_70(20)
	    end map
	  end union
	end structure
	record /data_arrays/	data_storage	!data record defined
	common /data_storage_common/ data_storage !put it all in common block

	... what ever ...
	data_storage.array_type = 1	!whatever is needed for typing data
	data_storage.i_by_2(10) = 33	!set up the data using the approp.
					!  variables...
	... what ever ...
	call print_out			!go do whatever using the same record
					! layout as above. Thats why common
					! is good for this problem.
	stop
	end

Hope this helps solve the problem you are having.!! :-)

Paul D. Clayton - Manager Of Systems
TSO Financial - Horsham, Pa. USA
Address - CLAYTON%XRT@CIS.UPENN.EDU