<?xml version="1.0" encoding="utf-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom">
	<channel>
		<title><![CDATA[Approximatrix Forums — Passing strings from Fortran to other languages]]></title>
		<link>http://forums.approximatrix.com/viewtopic.php?id=914</link>
		<atom:link href="http://forums.approximatrix.com/extern.php?action=feed&amp;tid=914&amp;type=rss" rel="self" type="application/rss+xml" />
		<description><![CDATA[The most recent posts in Passing strings from Fortran to other languages.]]></description>
		<lastBuildDate>Fri, 22 Sep 2023 20:56:44 +0000</lastBuildDate>
		<generator>PunBB</generator>
		<item>
			<title><![CDATA[Re: Passing strings from Fortran to other languages]]></title>
			<link>http://forums.approximatrix.com/viewtopic.php?pid=4202#p4202</link>
			<description><![CDATA[<p>When I&#039;m passing strings to C from Fortran, I usually try to be careful.&nbsp; This task is difficult because C doesn&#039;t formally have a definition of a string, just an array (actually just a pointer to a memory block) that happens to end in a null character (and only if we&#039;re talking about ASCII...).&nbsp; Fortran, on the other hand, knows a lot more about arrays and string lengths, most notably their dimensions.</p><p>When I&#039;m feeling like shooting from the hip, a string from Fortran can be passed pretty easily if you include that null character, something like:</p><div class="codebox"><pre><code>    interface
       function strlen(str) bind(c)
       use iso_c_binding
       character(len=*)::str
       integer(kind=c_int)::strlen
       end function strlen
    end interface

    ...
    
    i = strlen(trim(fortran_string)//c_null_char)</code></pre></div><p>So the above should &quot;work&quot; in most cases, but there are problems.&nbsp; What you&#039;re talking about is passing the ASCII values.&nbsp; I agree, that&#039;s the most robust way.&nbsp; But none of the values will exceed 255, so we might as well use CHARACTER instead of integer.&nbsp; The most correct way, in my opinion, is using an array of CHARACTERs with a KIND of C_CHAR and a TARGET attribute for passing so we can pass a true C pointer.&nbsp; It&#039;s wordy, but it makes me feel better:</p><div class="codebox"><pre><code>    use iso_c_binding

    character(kind=c_char), dimension(40)::c_passing
    integer::i

    interface
       function strlen(str) bind(c)
       use iso_c_binding
       type(c_ptr), value::str
       integer(kind=c_int)::strlen
       end function strlen
    end interface

    ...
    ! Fill the array completely with zeroes
    c_passing = c_null_char

    ! Fill the array with the string components, but make sure we have a zero on the end
    do i = 1, min(len_trim(fortran_string), size(c_passing)-1 )
        c_passing(i) = fortran_string(i:i)   ! &lt;- to transfer, character by character, into the C passing array
    end do

    i = strlen(c_loc(str))</code></pre></div><p>The above code treats C strings, in Fortran, exactly the same way C would treat them, so I guess it&#039;s technically more correct than the earlier example.&nbsp; But we&#039;re basically doing exactly what you proposed.</p>]]></description>
			<author><![CDATA[null@example.com (jeff)]]></author>
			<pubDate>Fri, 22 Sep 2023 20:56:44 +0000</pubDate>
			<guid>http://forums.approximatrix.com/viewtopic.php?pid=4202#p4202</guid>
		</item>
		<item>
			<title><![CDATA[Passing strings from Fortran to other languages]]></title>
			<link>http://forums.approximatrix.com/viewtopic.php?pid=4195#p4195</link>
			<description><![CDATA[<p>Regarding the interoperability of Fortran with C and other languages, it seems that even Fortran 2018 has not worked out all the bugs regarding sending a string from one language to a Fortran DLL, and then sending it from Fortran back to the calling language.</p><p>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) equivalents, and then send between C/C#/VB.NET/et al&nbsp; and Fortran as a 1D integer array?&nbsp; </p><p>For example, instead of trying to send &quot;This is a string&quot; between the two languages, why not send </p><p>(84, 104, 105, 115, 32, 105, 115, 32, 97, 32, 115, 116, 114, 105, 110, 103) </p><p>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 into ASCII, connotation of characters, etc.)? I am sure I am not the first one to think of this, but it seems a lot easier to embed the code to do the ASCII conversions in the two programs than to jump through hoops via iso C binding for strings.</p><p>Thoughts?&nbsp; Could it be that simple?</p>]]></description>
			<author><![CDATA[null@example.com (Lewist57)]]></author>
			<pubDate>Wed, 20 Sep 2023 22:18:47 +0000</pubDate>
			<guid>http://forums.approximatrix.com/viewtopic.php?pid=4195#p4195</guid>
		</item>
	</channel>
</rss>
