Home     |     .Net Programming    |     cSharp Home    |     Sql Server Home    |     Javascript / Client Side Development     |     Ajax Programming

Ruby on Rails Development     |     Perl Programming     |     C Programming Language     |     C++ Programming     |     IT Jobs

Python Programming Language     |     Laptop Suggestions?    |     TCL Scripting     |     Fortran Programming     |     Scheme Programming Language


 
 
Cervo Technologies
The Right Source to Outsource

MS Dynamics CRM 3.0

Fortran Programming Language

C- Fortran interface - Variable number of arguments


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

On 11 mei, 11:23, abaru@gmail.com 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

Arjen Markus wrote:

(snip on C varargs functions)

> 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.

C allows varargs routines do use a different calling mechanism,
but many use the same calling mechanism if it works.  With the
popular 'push all the arguments on the stack' method, it should
work fine.  Many now pass some arguments in registers, and in
some cases the varargs routine then push them onto the stack so
they can be indexed as before.

Assuming that the calling method are compatible and that the
Fortran compiler doesn't refuse to compile them, it could work.

-- glen

abaru@gmail.com 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

See here:

http://softwarecommunity.intel.com/isn/Community/en-US/forums/thread/...

I'm fairly sure it isn't portable, but you can give it a try.

--
 Jugoslav
___________
www.xeffort.com

Please reply to the newsgroup.
You can find my real e-mail on my home page above.

Add to del.icio.us | Digg this | Stumble it | Powered by Megasolutions Inc