Community
cancel
Showing results for 
Search instead for 
Did you mean: 
Highlighted
Beginner
22 Views

Problem with array mixing C++ and Fortran

Hello.

I have a Fortran static Library with one subroutine. I call that subroutine from C++ and I pass one array, inside the subroutine the array change its values but when I use it again in C++ I just get a single value.

Here it's the code 

C++

#include "stdafx.h"
#include "iostream"
using namespace std;

extern "C"{
	void LIBRERIA(int *ENTERO, double *DOBLE, double *ARREGLO);
}


int _tmain(int argc, _TCHAR* argv[])
{
	int ent = 5;
	double dou = 2;
	double arre[5];

	LIBRERIA(&ent, &dou, arre);

	cout << *arre << endl;
}

Fortran subroutine

Subroutine Libreria(entero, doble, arreglo)
    
    implicit none
    
    integer entero
    double precision doble, numerito
    double precision, dimension(entero) :: arreglo
    
    print*, "Inside the library"
    
    numerito = entero * doble
    arreglo(:) = numerito
    
    print*, arreglo
    
    
    End subroutine

Thanks for any help.

0 Kudos
1 Reply
Highlighted
Black Belt
22 Views

Your C++ code is only writing a single value to the console.  If you want to write all the elements of `arre`, then you need a loop or similar.

#include "stdafx.h"
#include <iostream>
using namespace std;

extern "C"{
	void LIBRERIA(int *ENTERO, double *DOBLE, double *ARREGLO);
}


int _tmain(int argc, _TCHAR* argv[])
{
	int ent = 5;
	double dou = 2;
	double arre[5];

	LIBRERIA(&ent, &dou, arre);

	for (int i = 0; i < ent; ++ i) cout << arre << endl;
}

 

For robustness, you should consider using Fortran 2003's C interoperability features in your Fortran code.

0 Kudos