I’m trying to access an array within a Fortran common block structure from C++.
I have a mixed sample code with C++ and Fortran.
Fortran:
integer a(5),b
common /sample/ a,b
a(1) = 1
a(2) = 5
a(3) = 10
a(4) = 15
a(5) = 20
b = 25
Then in C++:
extern "C"{
extern struct{
int *a,b;
}sample_;
From C++, if I try to print the value sample_.b:
printf("sample b:%d\n",sample_.b);
I get the value of a(2) : sample b:5
And if I try to print any other of the array-a values I just get a segementation fault…
printf("sample_.a[1]=%d\n",(int)sample_.a[1]);
printf("sample_.a[0]=%d\n",(int)sample_.a[0]);
What am I doing wrong?¿ Any idea ¿?
I think, maybe I have to pass the length of the array “a” too to C++, but if so, I don’t know how to do it either.
My Fortran is a bit (OK, QUITE a bit) rusty, but let me give it a whirl.
aprobably received the VALUE rather than a pointer to it. That would set it to1, which not a good pointer value.breceived the second value in the data block. Your C++ struct gives no indication to the compiler what the real format of the data is, so it’s going to just mindlessly assign the items to the structure in the order given in the struct. I’d get a pointer to the data block and disassemble it by hand.Assign
ato the ADDRESS of the data block (as along intpointer, looks like; your mileage may vary) andb = a[5]. Hope that helps.