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

passing array of strings between C++ and FORTRAN in mixed programming

rthimmana
Beginner
884 Views
Hi..
I have been working on this mixed programming problem for some time now. The working version what i had called the FORTRAN program which wrote the outputs to file on the hard disk. However i was wondering whether some time can be saved in I/O by transferring the strings of data in arrays rather than with FILE I/O.
However, i read in a few manuals that passing character pointers as parameters is not a good idea in mixed programming with C++ and Fortran.
I need guidance in solving this problem. Can strings be passed as parameters between C++ and FORTRAN..
Thanks in advance.
Raghavendher Thimmana.
0 Kudos
3 Replies
TimP
Honored Contributor III
884 Views
You probably have sufficient reason to bite the bullet and learn about one of the ways for the platforms you have chosen. Unfortunately, there was no Fortran standard on this until f2003, and that applies only to the extern "C" linkage from the C++ side. Just recently, there was a thread on this forum about an extension for C strings which is supported by some, but not all, Fortran compilers. So, you're best off checking advice specific to the compilers you intend to use. There are all kinds of .pdf, .chm, .xml, and .html files pertaining to Intel compilers.

A standard Fortran character string has a separate length parameter, while C and C++ expect an appended null character, not provided by most Fortrans. You should be able to use strncpy(), for example, to make a null terminated string from the Fortran one, and strlen() or the like to generate the length parameter to pass back to Fortran along with a C string.
0 Kudos
rthimmana
Beginner
884 Views
thanks for the reply..
the length of the string can be obtained from the hidden parameter passed in Fortran.
However, the problem i am working on needs strings to be written to an array of unknown length. But, array implementation in FORTRAN is column order which makes it hard to comprehend how the situation can be handled.
Can you provide me links for the .pdf or .html files regarding this issue.
thanks in advance.
raghavendher thimmana
0 Kudos
TimP
Honored Contributor III
884 Views
If you are actually talking about multiple strings in an array, the advice to avoid interoperating with C may have been well taken. In Fortran, character strings in an 1-D array would be expected to fall one after the other, with blank padding to make them all the same length. It would appear not too difficult on the C side to treat it as whichever type of char array you choose and count the trailing blanks in each element.

If you are talking about ordinary individual character strings, I suppose you found the section about Passing Arguments in Mixed-Language Programming in the help files which come with Intel Fortran.

I'm afraid I'm not sure enough of your question to understand what additional information you need.
0 Kudos
Reply