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

Syntax for passing character arrays from C to Fortran?

ferrad
New User
349 Views
I am trying to pass the program arguments from a C program to a Fortran DLL. On the C side I have:
Code:
extern void __stdcall aSimplexDriver_FTN(int argc, char *argv[]);
main(int argc, char *argv[])
{
      ASIMPLEXDRIVER_FTN(&argc, argv[0]);
}

and on the Fortran side:
Code:
subroutine aSimplexDriver_FTN(argc, argv)
!DEC$ ATTRIBUTES DLLEXPORT::aSimplexDriver_FTN
    implicit none
    integer argc
    character*(*) argv(0:1)
    ...
    return
end subroutine aSimplexDriver_FTN

I know that the character array passing syntax is incorrect: I know I need the char lengths in the calls somewhere. How do I correctly pass the char*[] array to Fortran?

Adrian

0 Kudos
1 Reply
Jugoslav_Dujic
Valued Contributor II
349 Views
There's no good solution for direct passing of char* argv[]. A semi-correct (VF-specific) Fortran translation of it would be:
TYPE CharArg
CHARACTER(LEN=?), POINTER:: pChar
END TYPE CharArg
SUBROUTINE aSimplexDriver_FTN(argc, argv)
TYPE(CharArg):: argv(argc)
Since you don't know the length (marked ?) in advance, you could use a "maximal" length and only access the string up to the first trailing �.


An IMO better solution would be to "unwind" argv into several separate arguments in C and pass those to Fortran.

Finally, in the special case of argc/argv, I think you don't have to pass them at all you should be able to retrieve them using NARGS and GETARG routines (but you should verify whether they work in a dll).

Jugoslav

Message Edited by JugoslavDujic on 01-09-2006 02:59 PM

0 Kudos
Reply