Re: C- Fortran interface - Variable number of arguments



On 11 mei, 11:23, abaru...@xxxxxxxxx wrote:
Hi
I am facing a problem while calling a C function with variable number
of arguments from Fortran. Since the C function has variable number
of arguments and also the range of arguments is not fixed it might be
difficult task to create a wrapper (although in C) to enable
callability from Fortran.
My C function is defined as

void addMessage(int msgNo, ...)
Here the three dots after the first mandatory argument is an essential
syntax for C variable arguments. Right now i am in a fix as to how to
go abt writing a wrapper for fortran. Any inputs or suggestions in
this regard are most welcome.
Regards
Arun

Fortran can not deal with such routines as you know and in C you
have to use special mechanisms (the varargs macros or whatever they
are called) to deal with variable-length argument lists.

How does your C routine know how many arguments were passed, is
that the msgNo argument? What kind of arguments are passed?

You could do it in this way:
- If they are all the same type, then pass them as an array in Fortran
and use a C function to split them into separate arguments:

addMessageF( int msgNo, int array[] ) {
switch (msgNo) {
case 1:
addMessage( 1, array[0] ) ;
break;
case 2:
addMessage( 2, array[0], array[1] );
break;
case 3:
..
default:
/* Error message */
}
}

Given that the interface does not have a format string or something
similar, my guess is the above solution is what you could use.

If there is a variety of argument types (i.e. the first can be an
integer,
the second a string or vice versa), you could use a derived type to
store all possibilities and pass an array of derived types to the
C wrapper. (Complicating factor is of course the fact that Fortran
derived types are not easily/portably dealt with in C)

Regards,

Arjen

.



Relevant Pages

  • FORTRAN <--> C#. Am I stuck with File IO?
    ... number-crunching FORTRAN routines. ... Because the output of the FORTRAN is largely variable-length, ... Some background info: ...
    (comp.lang.fortran)
  • Re: defined operator & assignment speed & memory usage problem
    ... resolve into a single multiply-add, and have it appear in much longer ... all of the projects except mine abandoned Fortran ... programming I'm describing. ... My work involves creating derived types that are reusable for solving ...
    (comp.lang.fortran)
  • Re: It Hurts When I Do This
    ... In this single instance, Fortran has it wrong. ... It leads ... but not for derived types. ... simillar underlying meaning. ...
    (comp.lang.fortran)
  • Re: Subroutine arguments vs module variables
    ... > use derived types to aggregate related arguments into a single ... var = sum**2)/size ... This would be a major break from traditional Fortran and perhaps could ... Returning several function results is a convenient feature of Python, ...
    (comp.lang.fortran)
  • Re: defining an arbitrary type in your newer Fortrans
    ... Everything is nicely handled by derived types. ... I don't see anything directly comparable in Fortran. ... Fortrans that have a macro preprocessor option, ... Comment: Using GnuPG with SUSE - http://enigmail.mozdev.org ...
    (comp.lang.fortran)