<?xml version="1.0" encoding="UTF-8"?>
<rss xmlns:content="http://purl.org/rss/1.0/modules/content/" xmlns:dc="http://purl.org/dc/elements/1.1/" xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#" xmlns:taxo="http://purl.org/rss/1.0/modules/taxonomy/" version="2.0">
  <channel>
    <title>topic Re: calling a c function from fortran in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650793#M174642</link>
    <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your reply&amp;nbsp;&lt;/P&gt;&lt;P&gt;so I can't pass a pointer on the fortran side ?&lt;/P&gt;&lt;P&gt;yes on the C side, the function allocates memory for the passed pointer&amp;nbsp;&lt;/P&gt;&lt;P&gt;best&lt;/P&gt;&lt;P&gt;jac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
    <pubDate>Thu, 19 Dec 2024 14:13:54 GMT</pubDate>
    <dc:creator>JacobB</dc:creator>
    <dc:date>2024-12-19T14:13:54Z</dc:date>
    <item>
      <title>calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650439#M174629</link>
      <description>&lt;P&gt;Hello&amp;nbsp;&lt;/P&gt;&lt;P&gt;I have a c function which receives an array of doubles and make changes to it.&amp;nbsp;&lt;/P&gt;&lt;P&gt;for example:&lt;/P&gt;&lt;P&gt;on the "c" side&amp;nbsp;&lt;/P&gt;&lt;P&gt;void DoSomething (double **values);&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;on the "fortran" side I have&amp;nbsp;&lt;/P&gt;&lt;P&gt;real*8 values(*)&lt;/P&gt;&lt;P&gt;pointer(pvalues,values)&lt;/P&gt;&lt;P&gt;call DoSomething(pvalues)&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;in order to make the connection I create a module with and interface&amp;nbsp;&lt;/P&gt;&lt;P&gt;module f_calling_c&lt;/P&gt;&lt;P&gt;interface&amp;nbsp;&lt;/P&gt;&lt;P&gt;subroutine DoSomething(pvalues) Bind(C,Name="DoSomething")&lt;/P&gt;&lt;P&gt;USE , INTRINSIC :: ISO_BINDING&lt;/P&gt;&lt;P&gt;REAL (KIND=C_DOUBLE), pointer :: pvalues&lt;/P&gt;&lt;P&gt;end subroutine DoSomething&amp;nbsp;&lt;/P&gt;&lt;P&gt;end Interface&lt;/P&gt;&lt;P&gt;end module f_calling_c&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;when I compile the fortran code I get the following errors&lt;/P&gt;&lt;P&gt;error #6633 the type of the actual argument differs from the type of the dummy argument [pvalues]&lt;/P&gt;&lt;P&gt;error #7496 a non-pointer actual argument shall have a target attribute when associated with a pointer dummy argument [pvalues]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;I know I am doing something wrong&amp;nbsp;&lt;/P&gt;&lt;P&gt;any help would really be appreciated&amp;nbsp;&lt;/P&gt;&lt;P&gt;best&lt;/P&gt;&lt;P&gt;jac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 12:35:15 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650439#M174629</guid>
      <dc:creator>JacobB</dc:creator>
      <dc:date>2024-12-18T12:35:15Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650451#M174630</link>
      <description>&lt;P&gt;The problem is that Fortran pointers are not equivalent to C pointers. As you have a double indirection - double **values - the type on the Fortran side should be a generic one, type(*). By NOT specifying the attribute "value" on the Fortran side, you make sure that on the C side it can be seen as a void ** argument and that is what you need.&lt;/P&gt;&lt;P&gt;Caveat: I have not tested this ;).&lt;/P&gt;&lt;P&gt;If the argument on the C side had been double *values, then the Fortran side might have been:&lt;/P&gt;&lt;P&gt;real(c_double), dimension(*) :: values&lt;/P&gt;&lt;P&gt;or:&lt;/P&gt;&lt;P&gt;real(c_double), intent(inout) :: values&amp;nbsp;&lt;/P&gt;&lt;P&gt;(I have added the intent just to show you the ambiguity of C pointers)&lt;/P&gt;&lt;P&gt;Is the C function allocating the array of values? That would present different difficulties.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;Before I continue speculating, could you explain what the C function actually does? There is ample ambiguity here and on the Fortran side you may need to be specific.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Wed, 18 Dec 2024 13:43:47 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650451#M174630</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2024-12-18T13:43:47Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650793#M174642</link>
      <description>&lt;P&gt;Hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks for your reply&amp;nbsp;&lt;/P&gt;&lt;P&gt;so I can't pass a pointer on the fortran side ?&lt;/P&gt;&lt;P&gt;yes on the C side, the function allocates memory for the passed pointer&amp;nbsp;&lt;/P&gt;&lt;P&gt;best&lt;/P&gt;&lt;P&gt;jac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 14:13:54 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650793#M174642</guid>
      <dc:creator>JacobB</dc:creator>
      <dc:date>2024-12-19T14:13:54Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650798#M174643</link>
      <description>&lt;P&gt;You can pass C pointers to and from Fortran,&amp;nbsp; that is not the problem. The thing is that an argument of type "double *" in a C function could mean two things (maybe even more): it is the starting address of an array of double-precision numbers or it is the address of a scalar and the function will change its value. In your case the C function allocates an array and returns the address. If you need to deallocate the memory, then that should be done on the C side.&lt;/P&gt;&lt;P&gt;To use the array in Fortran, use code like:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;call c_f_pointer( c_ptr, farray [size] )&lt;/LI-CODE&gt;&lt;P&gt;The interface of the C function may be:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;interface
subroutine DoSomething( allocated_ptr ) bind(C,name="DoSomething")
    use iso_c_binding, only: c_ptr
    type(c_ptr), intent(out) :: allocated_ptr
end subroutine DoSomething
end interface&lt;/LI-CODE&gt;&lt;P&gt;Since Fortran passes arguments by reference (by default, that is), you have a double indirection as required in this way.&lt;/P&gt;</description>
      <pubDate>Thu, 19 Dec 2024 14:47:06 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1650798#M174643</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2024-12-19T14:47:06Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651100#M174670</link>
      <description>&lt;P&gt;hi&amp;nbsp;&lt;/P&gt;&lt;P&gt;thanks a lot for your support . True that pointers in c/c++ can be ambiguous&lt;/P&gt;&lt;P&gt;I tried what you suggested&amp;nbsp;&lt;/P&gt;&lt;P&gt;module fci&lt;/P&gt;&lt;P&gt;interface&lt;BR /&gt;subroutine do_something (pvol,n) bind(C,NAME="do_something")&lt;BR /&gt;use iso_c_binding, only: c_ptr , c_int&lt;BR /&gt;type(c_ptr), intent(out) :: pvol&lt;BR /&gt;integer(KIND=C_INT), intent(in) :: n&lt;BR /&gt;end subroutine do_something&lt;BR /&gt;end interface&lt;/P&gt;&lt;P&gt;end module fci&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;the fortran function is as follows&lt;/P&gt;&lt;P&gt;use fci&lt;BR /&gt;pointer (ipvol, vol)&lt;BR /&gt;real*8 vol(*)&lt;BR /&gt;call do_something(pvol,10)&lt;/P&gt;&lt;P&gt;the module compiles fine but the main code does&amp;nbsp; not&amp;nbsp;&lt;/P&gt;&lt;P&gt;I get the following error&amp;nbsp;&lt;/P&gt;&lt;P&gt;error #6633: The type of the actual argument differs from the type of the dummy argument. [PVOL]&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;&lt;P&gt;it seems that there is need to specify the type of the pointer but I do not know how&amp;nbsp;&lt;/P&gt;&lt;P&gt;best&lt;/P&gt;&lt;P&gt;jac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2024 07:19:06 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651100#M174670</guid>
      <dc:creator>JacobB</dc:creator>
      <dc:date>2024-12-20T07:19:06Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651111#M174671</link>
      <description>&lt;P&gt;You are using a so-called Cray pointer - an extension that has been superseded by the pointers that were introduced in the Fortran 90 standard. What you should do instead is something along these lines:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;use fci
use iso_c_binding
type(c_ptr) :: pvol
real(kind=kind(1.0d0)), pointer :: vol(:)
call do_something(pvol,10)
call c_f_pointer(pvol, vol, [10])&lt;/LI-CODE&gt;&lt;P&gt;Also: the declaration of n in the interface is probably best done as:&lt;/P&gt;&lt;LI-CODE lang="markup"&gt;integer(kind=c_int), value :: n&lt;/LI-CODE&gt;&lt;P&gt;as the C function likely uses "int n" - which is a value, not a reference to an intent(in) variable.&lt;/P&gt;&lt;P&gt;Anyway, I took your code and the above fragment and created a working program ;).&lt;/P&gt;&lt;P&gt;And instead of REAL*8 I use the KIND= attribute, that is standard, whereas REAL*8 is a very common extension but an extension.&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2024 07:42:11 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651111#M174671</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2024-12-20T07:42:11Z</dc:date>
    </item>
    <item>
      <title>Re: calling a c function from fortran</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651118#M174672</link>
      <description>&lt;P&gt;Hi&lt;/P&gt;&lt;P&gt;Now I got it&amp;nbsp;&lt;/P&gt;&lt;P&gt;thank you very much&amp;nbsp;&lt;/P&gt;&lt;P&gt;jac&lt;/P&gt;&lt;P&gt;&amp;nbsp;&lt;/P&gt;</description>
      <pubDate>Fri, 20 Dec 2024 07:53:21 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/calling-a-c-function-from-fortran/m-p/1651118#M174672</guid>
      <dc:creator>JacobB</dc:creator>
      <dc:date>2024-12-20T07:53:21Z</dc:date>
    </item>
  </channel>
</rss>

