- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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":
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:
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?
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?
Link Copied
2 Replies
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
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".
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".

Reply
Topic Options
- Subscribe to RSS Feed
- Mark Topic as New
- Mark Topic as Read
- Float this Topic for Current User
- Bookmark
- Subscribe
- Printer Friendly Page