Hello!
I have a problem with the mixed languages programming.
This is a task with the cpp/fortran pointers passing when the 2d dynamic array was created.
Main CPP program is calling FORTRAN array filling subroutine. Can't get the right results, please help.
PS I can't using an one-dimensional array, like this arr[i*N +j] :(
CPP TEXT:
#include <cstdlib>
#include <iostream>
using namespace std;
extern "C" void fill_array(int **, int, int);
int main()
{
int i, j, N, M;
int **arr;
cout<<endl<<"rows Number ="<<endl; cin>>M;
cout<<endl<<"cols Number ="<<endl; cin>>N;
cout<<endl;
arr = new int*[N];
for(int i = 0; i < N; i++)arr[i] = new int[M];
//
fill_array(arr,M,N);
//
for( i = 0; i < N; i++)
{
for( j = 0; j < M; j++)
{
cout << arr[i][j]<<"";
}
cout<<endl;
}
//
for( i = 0; i < N; i++)delete [] arr[i];
delete [] arr;
return EXIT_SUCCESS;
}
FORTRAN TEXT:
SUBROUTINE FILL_ARRAY(C,M,N)BIND(C,name="fill_array")
USE, INTRINSIC :: ISO_C_BINDING
INTEGER(KIND=C_INT), INTENT(IN), VALUE :: N,M
TYPE(C_PTR) :: C
INTEGER(C_INT), POINTER :: F(:,:)
INTEGER :: I,J, RANK
RANK = N*M
CALL C_F_POINTER(C,F,[RANK])
DO I=1, M
DO J = 1, N
F(I, J) = I + J
ENDDO
ENDDO
END SUBROUTINE
Typical output for matrix 3x3 is not correct:
2 3 4
0 3 4
0 0 4