I have the following Fortran subroutine named show_value
that calls a C function named show_value
:
INTERFACE
SUBROUTINE show_value(variable) BIND(C, name = "show_value")
USE, INTRINSIC :: iso_c_binding
TYPE(*) :: variable
END SUBROUTINE
END INTERFACE
The C function show_value
:
void show_value(const void *variable)
{
printf("%d\n", *(int *) variable);
}
The Fortran subroutine works well when passing scalars to it. Example:
INTEGER :: x
x = 12
call show_value(x)
This will call the C function show_value
and print 12
, which is correct.
Now, according to the Fortran documentation, if one wants to enable subroutine show_value
to receive also arrays (of any dimensions) and not just scalars, the line TYPE(*) :: variable
should be changed to TYPE(*), DIMENSION(..) :: variable
.
After making this change, when executing the following Fortran code:
INTEGER, DIMENSION(3) :: y
y(1) = 15
y(2) = 17
y(3) = 19
call show_value(y)
The C function show_value
no longer prints the correct message (i.e. prints random numbers). Moreover, I found out that the address that the C function receives is 528 lower than the original (in Fortran). To confirm this:
void show_value(const void *variable)
{
printf("%d\n", *(int *) (variable + 528));
}
... which prints 15
(correct number).
Any idea what is going here?
Environment: Ubuntu 14.04 64 bit, gfortran 4.9