- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
I need to pass 2d arrays from C to Fortran using interoperability features.
in C :
arrays are declared as
int ** ia;
float ** fa;
double **da;
int m, n; // dimensions [m x n]
allocation is done as :
ia = (int **)malloc(sizeof(int *) * m);
for(i=0; i!=m; ++i) ia = (int *) malloc(sizeof(int) * n);
....
// call fortran subroutine
fortran_call(ia, fa, da);
in FORTRAN:
subroutine fortran_call(ia, fa, da, m, n) bind(c)
use iso_c_binding
integer(c_int), intent(in) :: ia(n,m)
real(c_float), intent(in) :: fa(n,m)
real(c_double), intent(in) :: da(n,m)
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
!!! the arrays are not passed to Fortran subroutine correctly !!!
!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
end subroutine
Could you explain me please how to pass 2d arrays (in form : iso_c_type **) to FORTRAN, please ?
Thank you so much !
Link Copied
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content
coolweather wrote:No prototype for fortran_call shown. This call has three arguments. ia here is an array of pointers to arrays of int. This allows what is known as a "ragged array" - ia[0] could point to an array of size 1, ia[2] could point to an array of size 100. (Note "could" - you've not done that, but the capability is there). The storage for the array pointed at by ia[0] and the storage for the array pointed at by ia[1] will probably not be adjacent in memory - they are separate "malloc's".I need to pass 2d arrays from C to Fortran using interoperability features.
in C :
[cpp] int ** ia; float ** fa; double **da; int m, n; // dimensions [m x n] // allocation is done as : ia = (int **)malloc(sizeof(int *) * m); for(i=0; i!=m; ++i) ia = (int *) malloc(sizeof(int) * n); // .... // call fortran subroutine fortran_call(ia, fa, da); [/cpp]
arrays are declared as
Five arguments. No declarations for m and n shown.in FORTRAN:
[fortran]subroutine fortran_call(ia, fa, da, m, n) bind(c) [/fortran]
[fortran] use iso_c_binding integer(c_int), intent(in) :: ia(n,m) [/fortran]ia here is a rectangular array (all rows must be the same length). In memory the data for the array is stored as a contiguous sequence. From a C point of view, this is equivalent to a single malloc, with the programmer then managing the concepts of rows and columns "ia[ix * ny + iy]". In terms of the matching C procedure the argument would be a "int *" (or equivalently an int[]). This is not consistent with the nature of the array and argument on the C side.
[fortran] real(c_float), intent(in) :: fa(n,m) real(c_double), intent(in) :: da(n,m) !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! !!! the arrays are not passed to Fortran subroutine correctly !!! !!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!! end subroutine [/fortran]Do you want a ragged array, or a rectangular array?Could you explain me please how to pass 2d arrays (in form : iso_c_type **) to FORTRAN, please ?
Thank you so much !
- Mark as New
- Bookmark
- Subscribe
- Mute
- Subscribe to RSS Feed
- Permalink
- Report Inappropriate Content

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