Intel® Fortran Compiler
Build applications that can scale for the future with optimized code designed for Intel® Xeon® and compatible processors.
28456 Discussions

Problem with array mixing C++ and Fortran

Juan_David_V_
Beginner
342 Views

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
IanH
Honored Contributor II
342 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
Reply