Re: Passing a 2 dimensional array from fortran to c++





glen herrmannsfeldt wrote:
> Rich Townsend wrote:
>
> > glen herrmannsfeldt wrote:
>
> (snip)
>
> >>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.
>
> > How so? Surey, x[10] has type (float *)? I thought that in C,
>
> > x[i]
>
> > is purely syntactic sugar for *(x+i). Am I wrong?
>
> It is *(x+i) but the compiler knows what it is applied to.
>
> float x[10][10];
>
> will allocate 100 contiguous float values, just like a Fortran array.
>
> When you say x[i][j] it computes 10*i+j, scaled by sizeof(float) to
> find the address of the array element. It will also do that for
>
> *(*(x+i)+j) for an array with that declaration.
>
> I believe the wording is that x[10] decays to (float*),
> but x is not, and does not decay to, (float**). You can't,
> for example, pass x to a subroutine with a float** dummy argument.
>
> As a function dummy argument declaration,
>
> float *y; and float y[]; are equivalent.
>
> float **x; and float x[][10]; are not.
>
> -- glen

First of all Thanks for all the replies
Yes, I have realised that float ** wouldnt do any things for me,
I was successful in passing the array like this, but the values werent
the same in the fortran code
I am pasting the code (simulated to explain my problem) so that you
could help me better

$cat main.cpp
#include <math.h>
#include <iostream.h>
#include <stdlib.h>

extern "C" {
void setup_(int *, int *);
void getfromfort_( float *);
void sendtofort_( float *);
}



int main ()
{

// Variable Declaration
int row, column, i;
float **arr ;

setup_( &row, &column);

// Dynamical allocation of the ysnout array
arr = new float*[row];

for (i=0; i < row; ++i)
arr[i]= new float[column];row

getfromfort_(&arr[0][0]);

/// Now I print the entire arr but i dont get the values i assigned in
fortran code,
//Instread i get all zeros


// Now i manipulated the array values and was trying to see if they
reflected in fortran
sendtofort_(&arr[0][0]);
// But some strange values seem to appear
return 0;
}


and my fortran code:

$cat fort.f90


module var
integer rows, columns
end module var

subroutine setup(row,column)
use var
integer row, column
! I do some stuff here which calculates rows and columns of the module
var
row = rows
column = columns

end subroutine


subroutine getfromfort(arr)

real *8 :: arr(rows,columns)
! I try and assign some values to the arr here, but it doesnt reflect
in my c++ code

end subroutine


subroutine sendtofort(arr)

real *8 :: arr(rows,columns)
! I try and print the array values now, but i dont get the array i
updated in my c++ code

end subroutine


I did not get any errors during compilation or execution, but the
result was not I wanted,
I even noticed that the fortran arrays are column majored and c++ row
majored.
Thanks for the help in advance

.



Relevant Pages

  • Re: VB .Net and Intel Visual Fortran 9
    ... having is that the VB program cannot load the DLL. ... subroutine) to accept an array from VB - in my case a 2D array. ... Declare Sub mmult Lib "test.dll" (ByRef i As Double, ByRef j As Double, ...
    (comp.lang.fortran)
  • VB .Net and Intel Visual Fortran 9
    ... having is that the VB program cannot load the DLL. ... subroutine) to accept an array from VB - in my case a 2D array. ... Declare Sub mmult Lib "test.dll" (ByRef i As Double, ByRef j As Double, ...
    (comp.lang.basic.visual.misc)
  • Re: VB .Net and Intel Visual Fortran 9
    ... having is that the VB program cannot load the DLL. ... subroutine) to accept an array from VB - in my case a 2D array. ... Declare Sub mmult Lib "test.dll" (ByRef i As Double, ByRef j As Double, ...
    (comp.lang.basic.visual.misc)
  • Re: memory leak help!
    ... a loop from the main program. ... REPLACE THE NEW COMPLEX INTO ORIGINAL ARRAY x ... END OF SUBROUTINE SCEUA ... IMPLICIT NONE ...
    (comp.lang.fortran)
  • VB .Net and Intel Visual Fortran 9
    ... having is that the VB program cannot load the DLL. ... subroutine) to accept an array from VB - in my case a 2D array. ... Declare Sub mmult Lib "test.dll" (ByRef i As Double, ByRef j As Double, ...
    (comp.lang.fortran)