<?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 Fortran interface to call a C function that returns a pointer to an array in Intel® Fortran Compiler</title>
    <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200309#M151245</link>
    <description>&lt;P&gt;Back again after being away for quite a while...&lt;/P&gt;
&lt;P&gt;After much searching, I found what I believe to be the closest answer to my problem is on stackoverflow (SO) at &lt;A href="https://stackoverflow.com/questions/3852790/fortran-interface-to-call-a-c-function-that-return-a-pointer" target="_blank"&gt;https://stackoverflow.com/questions/3852790/fortran-interface-to-call-a-c-function-that-return-a-pointer&lt;/A&gt;, (posted nearly 10 years ago!)&lt;/P&gt;
&lt;P&gt;I quote this because using that example keeps the code simple and still illustrates my problem.&lt;/P&gt;
&lt;P&gt;I want to return an &lt;U&gt;array&lt;/U&gt; that has been created/memory allocated in C++ and be able to analyse the answer in Fortran, because that is where the bulk of the code for this application lies. My application goes off into C++ to produce the integer array answer and returns it to the Fortran program via the C interface. The original SO example used a single double precision variable as the return. I’ve changed it to integer because that is what I will be dealing with in my application. The example code (as changed) works.&lt;/P&gt;
&lt;P&gt;I have highlighted with comments the changes that I have tried to make to return an array pointer, but I’ve run out of ideas. (I could say, “Oh for the bad old days when I could just equivalence an integer to an iarray(1) and go beyond the size of the array”, but I won’t. It’s good to have coding protections, but sometimes it gets frustrating.)&lt;/P&gt;
&lt;P&gt;I am using Visual Studio 2017 and Intel Fortran parallel_studio_xe_2019_update5_composer.&lt;/P&gt;
&lt;P&gt;My modified example of the original SO code:&lt;/P&gt;
&lt;P&gt;[code here&lt;/P&gt;
&lt;P&gt;! ps_test_pointers.f90&lt;/P&gt;
&lt;P&gt;program foo&lt;/P&gt;
&lt;P&gt;&amp;nbsp; use, intrinsic :: iso_c_binding, only : c_ptr,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_f_pointer,&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_int&lt;/P&gt;
&lt;P&gt;&amp;nbsp; implicit none&lt;/P&gt;
&lt;P&gt;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; integer(c_int), pointer :: f_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; function foofunc() bind(c)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import :: c_ptr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; implicit none&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type(c_ptr) :: foofunc!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end function foofunc&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp; c_p = foofunc()&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call c_f_pointer(c_p, f_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; print *, f_p&lt;/P&gt;
&lt;P&gt;end program foo&lt;/P&gt;
&lt;P&gt;##########################################################&lt;/P&gt;
&lt;P&gt;// ps_test_pointersC.cpp : 'Subroutine' only.&lt;/P&gt;
&lt;P&gt;extern "C" {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int bar[3] = { 2, 3, 4 };&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *foofunc() {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return bar;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;]&lt;/P&gt;
&lt;P&gt;As I said above, the code works, in the sense that it prints out the first element of the array (‘2’).&lt;/P&gt;
&lt;P&gt;If I add the ‘(:)’ to the definition of f_p, the code compiles without error, but when I run it, the program fails with the run-time error: “forrtl: severe (408): fort: (7): Attempt to use pointer F_P when it is not associated with a target” at the line “call c_f_pointer(c_p, f_p)”.&lt;/P&gt;
&lt;P&gt;I have tried declaring c_p as an array (“c_p(:)”), but I get the same error in the same place.&lt;/P&gt;
&lt;P&gt;I have also tried calling c_p as an argument to a subroutine – still only using integers&lt;/P&gt;
&lt;P&gt;[code here&lt;/P&gt;
&lt;P&gt;! ps_test_pointers.f90&lt;/P&gt;
&lt;P&gt;program foo&lt;/P&gt;
&lt;P&gt;&amp;nbsp; use, intrinsic :: iso_c_binding, only : c_ptr,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_f_pointer,&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_int&lt;/P&gt;
&lt;P&gt;&amp;nbsp; implicit none&lt;/P&gt;
&lt;P&gt;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; integer(c_int), pointer :: f_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; subroutine foofunc(c_p) bind(c)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import :: c_ptr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; implicit none&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end subroutine foofunc&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call foofunc(c_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call c_f_pointer(c_p, f_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; print *, f_p&lt;/P&gt;
&lt;P&gt;end program foo&lt;/P&gt;
&lt;P&gt;##########################################################&lt;/P&gt;
&lt;P&gt;// ps_test_pointersC.cpp : 'Subroutine' only.&lt;/P&gt;
&lt;P&gt;extern "C" {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int bar[3] = { 2, 3, 4 };&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void foofunc(int *rtn) {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtn = bar;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;]&lt;/P&gt;
&lt;P&gt;but the created pointer in the C function never gets assigned to c_p on return (hence f_p is never defined).&lt;/P&gt;
&lt;P&gt;Reading around the problem, I hope I’m not at the bleeding edge of compiler implementation and have exposed a problem between restrictions tightening but not coping with all the use cases!&lt;/P&gt;
&lt;P&gt;Is there a solution to this? Any?&lt;/P&gt;
&lt;P&gt;Contributions will be gratefully received!&lt;/P&gt;</description>
    <pubDate>Fri, 14 Aug 2020 11:25:58 GMT</pubDate>
    <dc:creator>GeoffH</dc:creator>
    <dc:date>2020-08-14T11:25:58Z</dc:date>
    <item>
      <title>Fortran interface to call a C function that returns a pointer to an array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200309#M151245</link>
      <description>&lt;P&gt;Back again after being away for quite a while...&lt;/P&gt;
&lt;P&gt;After much searching, I found what I believe to be the closest answer to my problem is on stackoverflow (SO) at &lt;A href="https://stackoverflow.com/questions/3852790/fortran-interface-to-call-a-c-function-that-return-a-pointer" target="_blank"&gt;https://stackoverflow.com/questions/3852790/fortran-interface-to-call-a-c-function-that-return-a-pointer&lt;/A&gt;, (posted nearly 10 years ago!)&lt;/P&gt;
&lt;P&gt;I quote this because using that example keeps the code simple and still illustrates my problem.&lt;/P&gt;
&lt;P&gt;I want to return an &lt;U&gt;array&lt;/U&gt; that has been created/memory allocated in C++ and be able to analyse the answer in Fortran, because that is where the bulk of the code for this application lies. My application goes off into C++ to produce the integer array answer and returns it to the Fortran program via the C interface. The original SO example used a single double precision variable as the return. I’ve changed it to integer because that is what I will be dealing with in my application. The example code (as changed) works.&lt;/P&gt;
&lt;P&gt;I have highlighted with comments the changes that I have tried to make to return an array pointer, but I’ve run out of ideas. (I could say, “Oh for the bad old days when I could just equivalence an integer to an iarray(1) and go beyond the size of the array”, but I won’t. It’s good to have coding protections, but sometimes it gets frustrating.)&lt;/P&gt;
&lt;P&gt;I am using Visual Studio 2017 and Intel Fortran parallel_studio_xe_2019_update5_composer.&lt;/P&gt;
&lt;P&gt;My modified example of the original SO code:&lt;/P&gt;
&lt;P&gt;[code here&lt;/P&gt;
&lt;P&gt;! ps_test_pointers.f90&lt;/P&gt;
&lt;P&gt;program foo&lt;/P&gt;
&lt;P&gt;&amp;nbsp; use, intrinsic :: iso_c_binding, only : c_ptr,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_f_pointer,&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_int&lt;/P&gt;
&lt;P&gt;&amp;nbsp; implicit none&lt;/P&gt;
&lt;P&gt;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; integer(c_int), pointer :: f_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; function foofunc() bind(c)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import :: c_ptr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; implicit none&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type(c_ptr) :: foofunc!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end function foofunc&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp; c_p = foofunc()&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call c_f_pointer(c_p, f_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; print *, f_p&lt;/P&gt;
&lt;P&gt;end program foo&lt;/P&gt;
&lt;P&gt;##########################################################&lt;/P&gt;
&lt;P&gt;// ps_test_pointersC.cpp : 'Subroutine' only.&lt;/P&gt;
&lt;P&gt;extern "C" {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int bar[3] = { 2, 3, 4 };&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; int *foofunc() {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; return bar;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;]&lt;/P&gt;
&lt;P&gt;As I said above, the code works, in the sense that it prints out the first element of the array (‘2’).&lt;/P&gt;
&lt;P&gt;If I add the ‘(:)’ to the definition of f_p, the code compiles without error, but when I run it, the program fails with the run-time error: “forrtl: severe (408): fort: (7): Attempt to use pointer F_P when it is not associated with a target” at the line “call c_f_pointer(c_p, f_p)”.&lt;/P&gt;
&lt;P&gt;I have tried declaring c_p as an array (“c_p(:)”), but I get the same error in the same place.&lt;/P&gt;
&lt;P&gt;I have also tried calling c_p as an argument to a subroutine – still only using integers&lt;/P&gt;
&lt;P&gt;[code here&lt;/P&gt;
&lt;P&gt;! ps_test_pointers.f90&lt;/P&gt;
&lt;P&gt;program foo&lt;/P&gt;
&lt;P&gt;&amp;nbsp; use, intrinsic :: iso_c_binding, only : c_ptr,&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_f_pointer,&amp;nbsp; &amp;amp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; c_int&lt;/P&gt;
&lt;P&gt;&amp;nbsp; implicit none&lt;/P&gt;
&lt;P&gt;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; integer(c_int), pointer :: f_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp; interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; subroutine foofunc(c_p) bind(c)&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; import :: c_ptr&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; implicit none&amp;nbsp;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; type(c_ptr) :: c_p!(:) ! &amp;lt;-------&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; end subroutine foofunc&lt;/P&gt;
&lt;P&gt;&amp;nbsp; end interface&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call foofunc(c_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; call c_f_pointer(c_p, f_p)&lt;/P&gt;
&lt;P&gt;&amp;nbsp; print *, f_p&lt;/P&gt;
&lt;P&gt;end program foo&lt;/P&gt;
&lt;P&gt;##########################################################&lt;/P&gt;
&lt;P&gt;// ps_test_pointersC.cpp : 'Subroutine' only.&lt;/P&gt;
&lt;P&gt;extern "C" {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; int bar[3] = { 2, 3, 4 };&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; void foofunc(int *rtn) {&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp;&amp;nbsp; rtn = bar;&lt;/P&gt;
&lt;P&gt;&amp;nbsp;&amp;nbsp;&amp;nbsp; }&lt;/P&gt;
&lt;P&gt;}&lt;/P&gt;
&lt;P&gt;]&lt;/P&gt;
&lt;P&gt;but the created pointer in the C function never gets assigned to c_p on return (hence f_p is never defined).&lt;/P&gt;
&lt;P&gt;Reading around the problem, I hope I’m not at the bleeding edge of compiler implementation and have exposed a problem between restrictions tightening but not coping with all the use cases!&lt;/P&gt;
&lt;P&gt;Is there a solution to this? Any?&lt;/P&gt;
&lt;P&gt;Contributions will be gratefully received!&lt;/P&gt;</description>
      <pubDate>Fri, 14 Aug 2020 11:25:58 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200309#M151245</guid>
      <dc:creator>GeoffH</dc:creator>
      <dc:date>2020-08-14T11:25:58Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran interface to call a C function that returns a pointer to an array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200315#M151246</link>
      <description>&lt;P&gt;I think you are working a trifle too hard here:&lt;/P&gt;
&lt;P&gt;type(c_ptr) :: c_p(:) would be an array of C pointers, not a C pointer to a C array.&lt;/P&gt;
&lt;P&gt;type(cptr) :: c_p is the one you need, because C returns an array and whatever is behind that address is of no interest to the declaration on the Fortran side, because C does not pass on this information.&lt;/P&gt;
&lt;P&gt;To turn c_p into an array on the Fortran side use:&lt;/P&gt;
&lt;P&gt;call c_f_pointer( c_p, f_p, [3] )&amp;nbsp; ! [3] is the shape of the resulting Fortran array - fill in whatever is needed&lt;/P&gt;
&lt;P&gt;I may be overlooking something - I have not tried it, but this is the principle :).&lt;/P&gt;</description>
      <pubDate>Fri, 14 Aug 2020 12:16:02 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200315#M151246</guid>
      <dc:creator>Arjen_Markus</dc:creator>
      <dc:date>2020-08-14T12:16:02Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran interface to call a C function that returns a pointer to an array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200484#M151255</link>
      <description>&lt;P&gt;Thank you &lt;SPAN class="UserName lia-user-name lia-user-rank-Valued-Contributor-II lia-component-message-view-widget-author-username"&gt; &lt;A id="link_11" class="lia-link-navigation lia-page-link lia-user-name-link" href="https://community.intel.com/t5/user/viewprofilepage/user-id/75329" target="_self"&gt;&lt;SPAN class=""&gt;Arjen_Markus&lt;/SPAN&gt;&lt;/A&gt;&lt;/SPAN&gt;. That's part of the solution. I hinted that this example is a very simplified form of my problem that illustrates the issue. That said, I do have the size of the array in the returned pointer array and I can extract that and return it in the foofunc (or more accurately for a Fortran user, foosub).&lt;/P&gt;
&lt;P&gt;I still have some more work to do to get all the info back into the Fortran program, but you have broken my deadlock. Thanks.&lt;/P&gt;</description>
      <pubDate>Sat, 15 Aug 2020 01:35:43 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1200484#M151255</guid>
      <dc:creator>GeoffH</dc:creator>
      <dc:date>2020-08-15T01:35:43Z</dc:date>
    </item>
    <item>
      <title>Re: Fortran interface to call a C function that returns a pointer to an array</title>
      <link>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1202444#M151392</link>
      <description>&lt;P&gt;Yep. All working now.&lt;/P&gt;
&lt;P&gt;&lt;LI-EMOJI id="lia_slightly-smiling-face" title=":slightly_smiling_face:"&gt;&lt;/LI-EMOJI&gt;&lt;/P&gt;</description>
      <pubDate>Fri, 21 Aug 2020 11:25:19 GMT</pubDate>
      <guid>https://community.intel.com/t5/Intel-Fortran-Compiler/Fortran-interface-to-call-a-C-function-that-returns-a-pointer-to/m-p/1202444#M151392</guid>
      <dc:creator>GeoffH</dc:creator>
      <dc:date>2020-08-21T11:25:19Z</dc:date>
    </item>
  </channel>
</rss>

