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

a subroutine like READ (variable number of parameters)

jmdawk
Beginner
1,288 Views
I want to make a subroutine like the READ statement. By that I mean, I want to be able to use any number of parameters, and have them be of any type. The function will eventually call a c function which will be emulating the fortran READ statement, but will be getting the data from a C data structure.

So I was wondering if it is possible to make a fortran function that I could use like this:
CREAD (1, '(%i,%i,%d,%c,%d)', int1, int2, double1, char1, double2)
and then use the same subroutine like:
CREAD (1, '(%c,%i,%d,%d)', char2, int1, double1, double2)
etc, etc...

If this can be done, could someone point me in the right direction for some documentation on it?

The statement I'm after is similar to the use of ellipses in c function signatures.

Regards
Joel Dawkins
0 Kudos
1 Reply
james1
Beginner
1,288 Views
Fortran doesn't have a variable argument list in the sense that C does. However a decent solution is to make an vector of addresses, something like:

subroutine cread (unit, format, arguments)
integer(DWORD) unit
character(*) format
integer(POINTER_LEN) arguments(0:*)
...

where the first element of arguments is the variable parameter count. So calling looks something like:

arguments(0) = 5
arguments(1) = LOC(int1)
arguments(2) = LOC(int2)
arguments(3) = LOC(double1)
arguments(4) = LOC(char1)
arguments(5) = LOC(double2)
call cread (1, '(%i,%i,%d,%c,%d)', arguments)

James
0 Kudos
Reply