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

Passing strings to and from Fortran from other languages

lewist57
Novice
305 Views

It seems that even in Fortran 2018 that passing strings to and from Fortran from other languages is less than pleasant.

 

I have not put a lot of thought into it and have not tried this (yet), but why not simply convert the characters in a string into their ASCII (or other base) integer equivalents, and send between C (or other language)  and Fortran via a 1D integer array?

 

For example, instead of trying to send "This is a string" between the two languages, why not send

(84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103)

back and forth (of course using the appropriate routines in C [or other language] and Fortran to do the character to integer conversions, parsing of the string, connotation of characters, etc.)?

 

Could it be that simple, or are there unforeseen pitfalls?

0 Kudos
1 Reply
jimdempseyatthecove
Honored Contributor III
285 Views

jimdempseyatthecove_0-1695255706676.png

Post-fixing a literal with C declares the Fortran string to be C compatible.

The variable STRING shows a box in place of the NULL character.

To pass this on to a C/C++ function, use TRIM(string) to remove the trailing spaces.

 

Jim Dempsey

 

Reply