Re: C- Fortran interface - Variable number of arguments
- From: Arjen Markus <arjen.markus@xxxxxxxxxx>
- Date: 11 May 2007 02:53:40 -0700
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
.
- Follow-Ups:
- Re: C- Fortran interface - Variable number of arguments
- From: glen herrmannsfeldt
- Re: C- Fortran interface - Variable number of arguments
- References:
- C- Fortran interface - Variable number of arguments
- From: abarun22
- C- Fortran interface - Variable number of arguments
- Prev by Date: C- Fortran interface - Variable number of arguments
- Next by Date: Re: array of functions...
- Previous by thread: C- Fortran interface - Variable number of arguments
- Next by thread: Re: C- Fortran interface - Variable number of arguments
- Index(es):
Relevant Pages
|