Re: Passing a 2 dimensional array from fortran to c++
- From: glen herrmannsfeldt <gah@xxxxxxxxxxxxxxxx>
- Date: Wed, 29 Jun 2005 17:14:59 -0700
Sam wrote:
Hello all
I have a two dimensional array (the dimensions are not known)
> that needs to be passed to fortran from c++.
Say in my c++ code I have;
extern "C" { void foo_(float **, int &, int &); }
int main()
{
...........
.............
int rows, columns;
float **array2d; // Now i need to pass this to the fortran code, the
allocation of the array and
// and filling it is done in the fortran code.
// How do i pass this to the fortran subroutine now, i did it this way
foo_(array2d, rows, columns);
// One more question, do i need to do the allocation and> // stuff here again
// Do some stuff to manipulate with the array2d
(snip)
subroutine foo(arr,rows,columns) integer rows, columns real*8 ,allocatable:: arr (:,:) ! do some stuff to allocate the arrray and fil the stuff
First (float**) is not a two dimensional array. It is a pointer to pointer to float. It is often used in place of 2D arrays, but it still
isn't one. Some will even say that a C array declared like:
float x[10][10];
isn't a 2D array, either, but I will disagree.
As far as I know, there is nothing that can be done with a (float**) in a Fortran program.
The traditional Fortran assumed size array is usually done by passing the address of the array to the called routine.
Assumed shape arrays must have a descriptor that includes the dimension information, and also where to find the array elements, possibly in discontiguous storage.
Calling a Fortran routine with an assumed size argument passing a C pointer should work in many systems. If you really need a (float**)
you can allocate the entire array as one (float*) and then a separate
(float**) to index the columns (or rows) in the C program. Pass the
original (float*) to the Fortran routine.
Much less portably, you could find the form of the descriptor used for assumed shape arrays and create one as a C struct. It might be that a C struct with padding won't do, and that it must be constructed at runtime with (unsigned char*) and memcpy(). This can probably be done starting from a (float**).
In all those cases, the array(s) should be allocated using C's malloc().
Another possibility is to find the convention used for ALLOCATABLE by your Fortran system and arrange the C program to use that. In that case it would be allocated in Fortran.
-- glen
.
- Follow-Ups:
- Re: Passing a 2 dimensional array from fortran to c++
- From: Rich Townsend
- Re: Passing a 2 dimensional array from fortran to c++
- References:
- Prev by Date: Re: MODULEand USE versus Argument Passing
- Next by Date: Re: Passing a 2 dimensional array from fortran to c++
- Previous by thread: Passing a 2 dimensional array from fortran to c++
- Next by thread: Re: Passing a 2 dimensional array from fortran to c++
- Index(es):
Relevant Pages
|