This took me ages to figure out, so I hope it's of use to someone.
Firstly, each compiler is different; I am using GNU gcc on Linux. A CHARACTER variable, like any other, is passed between Fortran functions by reference, i.e. the calling C/C++ function should pass a char*.
Now Fortran uses a hidden length parameter for each CHARACTER passed. For gcc, these should go at the end of the parameter list, but for some compilers each length comes immediately after its string. I have even seen one example where it's not required at all; don't ask me what platform.
NB: unlike other fortran function parameters, this one is PASSED BY VALUE. I use type long in my example.
Finally, if you are calling a function which expects an array of such strings, you can put them into a single char[] array. You can pass a char* or a char[n][]; the linker won't care as long as your .h and .cpp files are consistent. The strings should not have C-style null-terminators but must be SPACE-PADDED to the required length. The 'required length' applies to all the strings, i.e. they must all be the same length which is indicated as usual by a single length parameter at the end of the list.
e.g. call TABIN with a single string and BRNSET with an array of strings:
Fortran file:
-------------
subroutine tabin(in,modnam)
character*(*) modnam
...
subroutine brnset(nn,pcntl,prflg)
logical prflg(3)
character*(*) pcntl(nn)
...
Header file:
------------
extern "C" void tabin_( long* nUnit, char* strModelName, long nStrLen );
extern "C" void brnset_( long* nPhases, char* strPhases, bool* bPrintFlags, long nStrLen );
Source file:
------------
tabin_( &nUnit, chModelName, strModel.length() );
brnset_( &nNoPhases, chPhases, bPrintFlags, 8L);
Cheers,
t
Firstly, each compiler is different; I am using GNU gcc on Linux. A CHARACTER variable, like any other, is passed between Fortran functions by reference, i.e. the calling C/C++ function should pass a char*.
Now Fortran uses a hidden length parameter for each CHARACTER passed. For gcc, these should go at the end of the parameter list, but for some compilers each length comes immediately after its string. I have even seen one example where it's not required at all; don't ask me what platform.
NB: unlike other fortran function parameters, this one is PASSED BY VALUE. I use type long in my example.
Finally, if you are calling a function which expects an array of such strings, you can put them into a single char[] array. You can pass a char* or a char[n][]; the linker won't care as long as your .h and .cpp files are consistent. The strings should not have C-style null-terminators but must be SPACE-PADDED to the required length. The 'required length' applies to all the strings, i.e. they must all be the same length which is indicated as usual by a single length parameter at the end of the list.
e.g. call TABIN with a single string and BRNSET with an array of strings:
Fortran file:
-------------
subroutine tabin(in,modnam)
character*(*) modnam
...
subroutine brnset(nn,pcntl,prflg)
logical prflg(3)
character*(*) pcntl(nn)
...
Header file:
------------
extern "C" void tabin_( long* nUnit, char* strModelName, long nStrLen );
extern "C" void brnset_( long* nPhases, char* strPhases, bool* bPrintFlags, long nStrLen );
Source file:
------------
tabin_( &nUnit, chModelName, strModel.length() );
brnset_( &nNoPhases, chPhases, bPrintFlags, 8L);
Cheers,
t