Hi Ian,
I have had problems with passing pointers from C to Fortran and back again. This might be a bug, but of course I might be doing something silly.
I skimmed your code and I think you are indeed, but I also think there
is little shame in falling into this particular trap. Been there, done
it myself!
Pointer location: 22040480
Value: 352
Pointer location (Fortran): 22040480
Pointer location: -47467344
Value: 22040480
Clearly at some point, pointer and value get mixed up here.
What the C caller does is to pass a copy of the pointer to the
function, so the callee is supposed to receive the pointer by value --
meaning the value of the pointer variable, not the value of the object
it points to. Now re-read that sentence :-)
The Fortran function OTOH expects to receive a c-pointer by
reference*, i.e. the usual Fortran way of passing arguments (*by
reference is not exactly true, but it behaves like that).
You should be able to fix your code by either passing the address of
the pointer variable (i.e. a pointer to the pointer) from C, or by
adding the VALUE attribute to the c_ptr dummy argument in Fortran, so
it expects a copy of the pointer variable, not a reference to it.
Personally, I prefer the latter option.
Cheers,
Dennis