Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
Announcements
FPGA community forums and blogs on community.intel.com are migrating to the new Altera Community and are read-only. For urgent support needs during this transition, please visit the FPGA Design Resources page or contact an Altera Authorized Distributor.
29280 Discussions

Accessing fortran module data from c

Lance_Larsen
Beginner
654 Views
I have a program where I need to access data in a fortran module from c.As in example, in the following module, I need to access "mighty":

MODULE testm
  TYPE mytype
    INTEGER :: sz
    INTEGER, POINTER :: j(:)
    INTEGER, POINTER :: k(:)
  END TYPE mytype
  
  TYPE(mytype), SAVE :: mighty
CONTAINS
  SUBROUTINE init()
    mighty%sz = 3
    ALLOCATE(mighty%j(mighty%sz))
    mighty%j = (/ 1,2,3 /)
    ALLOCATE(mighty%k(mighty%sz))
    mighty%k = (/ 4,5,6 /)    
  END SUBROUTINE INIT
END MODULE testm

I am using intel fortran and MSVC. The intel fortran documentation gave some indicationof how to access fortran module data from c, but seemed to leave some holes. I didn'tsee info on how to access pointers contained in types. I created a c header file with the following:

extern struct {
  int sz;
  int* j;
  int* k;
} TESTM_mp_MIGHTY;

I can access the 'sz' value and the 'j' array correctly from TESTM_mp_MIGHTY, but it lookslike my 'k' pointer is no good. In essence, anything following the 'j' pointer seems to beincorrect. Can anyone tell me what I need to do to access pointers in structures correctly so that the values that follow are accessed correctly?
0 Kudos
2 Replies
Lance_Larsen
Beginner
654 Views
Also, how do I access a 2d array such as:
  TYPE mytype
    INTEGER :: sz
    INTEGER, POINTER :: j(:)
    INTEGER, POINTER :: k(:)
    INTEGER, POINTER :: m(:,:)
  END TYPE mytype
0 Kudos
Steven_L_Intel1
Employee
654 Views
You can't do this. Fortran pointers are not the same as C pointers.

What you can do is have components in your derived type of TYPE(C_PTR) where the definition of C_PTR comes from module ISO_C_BINDING. You can then use C_LOC(array) to assign values to these components. A C_PTR is interoperable with a C pointer.

For arrays, you need to be aware that C indexes starting at zero and is row-major. C doesn't have the equivalent of Fortran multidimensional arrays - in C, multidimensional arrays are "arrays of pointers to arrays".
0 Kudos
Reply