2

Say I have a C function void foo(int* val) for which I have written the following Fortran C binding (F2008 standard conforming, I hope):

use, intrinsic :: iso_c_binding
...
subroutine foo(val) bind(c)
  integer(kind=c_int), intent(inout) :: val
end subroutine

The implementation of the C function void foo(int* val) treats the argument val as optional by checking the pointer for NULL. My question is: How can I set up and/or use the Fortran C binding subroutine foo(val) such that it supports val to be optional, i.e. such that actually a null pointer is passed to the C function?

  • The linked question talks about calling a Fortran procedure from C (or C++), but the concept is the same in reverse: use the `optional` attribute with a compiler supporting that feature of Fortran 2018. – francescalus Jul 29 '19 at 07:51
  • Just to be sure: If I declare my Fortran interface for `subroutine foo(val)` with `val` as `optional` and use it as `call foo()`, it will actually call the C function with `foo(0)`? Is there also any way I can make it work with Fortran 2008? – Michael Schlottke-Lakemper Jul 30 '19 at 15:29
  • @francescalus I kindly ask you to reconsider to rescind the duplicate flag, as the referenced post does not really answer my question (it is concerned with the opposite call direction and misses vital information such as that it only works with Fortran 2018). – Michael Schlottke-Lakemper Jul 30 '19 at 15:37
  • The "opposite call direction" isn't really an issue: under C-interoperability (as tagged [tag:fortran-iso-c-binding]) we have that symmetry. Regarding the feature difference between F2008 and F2018/F2008+TS, that is a valid point, but the point is just as appropriate for that question and could be answered there: please comment under the question/answer and we can provide a F2008 workaround if required. (This isn't to say your question isn't a good one, it's just that I view the duplicate system as a way to avoid putting related information in answers to several questions). – francescalus Jul 30 '19 at 16:09
  • I'll aim to put a suitable answer to that question addressing your points, in the next few hours. If I haven't, or you think it doesn't consider your points, then please ping me again. – francescalus Jul 30 '19 at 16:14

0 Answers0