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

how to receive a c++ pointer in Fortran

nrkssa
Beginner
319 Views
Hi all

I have a mixed C++ and Fortran code in which I need to pass the pointer to a linklist in c++ to a function in Fortran.
[cpp]// C++ part that returns a pointer extern "C"{ int* get_lscllist_(char ch){ extern _linklist link1; int *lastcell; lastcell=link1.getlscl(ch); return lastcell; } }[/cpp] [cpp]
[/cpp]
[fortran]!!!! Fortran Code with the interface to C++ function Function Check_minimum_distance(v) result(fres) Use, Intrinsic :: ISO_C_BINDING IMPLICIT NONE Interface Function get_lscllist(ch) Result(lscl) BIND(C,name="get_lscllist_") Use ISO_C_BINDING, ONLY:C_INT,C_CHAR,C_PTR Integer(C_INT),Pointer :: lscl Character(C_CHAR) ::ch End Function get_lscllist End Interface Integer::i,v,fres Integer,Pointer :: lscl(:) lscl=get_lscllist('m') Print*," Fortran address is",lscl Stop RETURN End Function Check_minimum_distance [/fortran]
[fortran]
[/fortran] [fortran]on running this code I get the following results:[/fortran] [fortran] C++ : C++ address is 0x2cab360[/fortran] [fortran]Fortran : Fortran address is -1 // returns the value instead of the address.[/fortran] [fortran]
[/fortran][fortran]I understand that Fortran passes and receives variables by address and hence I could see why the conversion happens. Is there[/fortran] [fortran]any modification I should make so that I can receive the address on the Fortran side ?[/fortran] [fortran]
[/fortran][fortran]Thanks in advance[/fortran] [fortran]Ram[/fortran]
0 Kudos
2 Replies
Udit_P_Intel
Employee
319 Views
You may need the VALUE attribute on LSCL. This will specify that LSCL is passed by value to the caller.
Best!
0 Kudos
JVanB
Valued Contributor II
319 Views
Your Fortran code is a train wreck with a few trucks piling on for good measure.In your interface body for get_lscllist(), does ifort really let you declare the result variable to be a Fortran pointer? Compile with standard checking enabled next time so that the compiler can disabuse you of the notion that a normal C function could return an object like that. What the C function does return is TYPE(C_PTR) :: lscl. Also the declaration of the dummy argument is just wrong. Character(C_CHAR) :: ch implies that dummy argument has LEN = C_CHAR and KIND = KIND('A'), also that it is passed by reference. The declaration matching your C code is Character(kind=C_CHAR), value :: ch. Then what do you suppose the Fortran code itself does when executing the line lscl=get_lscllist('m')? First, it saves the value 'm' at some address, then sends the address to get_lscllist(). On return it thinks it has received a Fortran pointer to a scalar integer and then broadcasts the value of the scalar integer pointed to to all elements of the array pointed to by lscl. Problem: lscl is not yet associated with any target, so where and to how many array elements is this value supposed to be broadcast? You seem to be hoping for pointer assignment (=>) when using ordinary assignment (=).
So fix the interface body as outlined above and change your Fortran source code so that it receives a value of TYPE(C_PTR) from get_lscllist() and then uses C_F_POINTER to convert that C_PTR to a Fortran pointer (whether a pointer to a scalar or anarray is required is not clear from your code).
0 Kudos
Reply