I am struggling with filling a string array in a C++ code. Here is what I tried to do:
main.f90:
program main
implicit none
character(len=100) arr(3)
call get_string_array(arr, 100, 3)
write(*,'(3(a,3x))') arr
end
cpp_func.cpp:
#include <cstring>
#include <algorithm>
using namespace std;
void ConvertToFortran(char* fstring, size_t fstring_len, const char* cstring)
{
size_t inlen = strlen(cstring);
size_t cpylen = min(inlen, fstring_len);
fill(fstring, fstring + fstring_len, ' ');
copy(cstring, cstring + cpylen, fstring);
}
extern "C"
{
void get_string_array_(char** arr, int len, int n)
{
const char* strings[] = {"duma1", "duma2", "duma3"};
for (int i = 0; i < n; i++)
ConvertToFortran(arr[i], len, strings[i]);
}
}
After compiling the two files as
ifx -c main.f90
icx -c cpp_func.cpp
ifx main.o cpp_func.o
I receive a segmentation fault. Could you please help me how to print array arr properly within main.f90?
*** Edit ***
Any solution which is able to pass a string array from C++ to Fortran would be appreciated.
bind(C)") and the second is to work out what the interoperable things are. If you can write using C interop with a clear statement of design goals, we'll be able to give more concrete advice.lenandn. (Again:bind(C).)