From: bio_amateur on
I have this function in C
void func1 (const int * arg1, const int arg2)

To call the C function in Fortran, I'm not sure which interface is
correct:

subroutine func1 (arg1, arg2)
integer(c_int), intent(IN) :: arg1
integer(c_int), intent(IN), value :: arg2
end

or

subroutine func1 (arg1, arg2)
integer(c_int), intent(IN) :: arg1
integer(c_int), value :: arg2
end

And the final one is how about this situation: const void* and const
void**
How we map these objects in Fortran?

Thanks,
Tuan
From: glen herrmannsfeldt on
bio_amateur <hoangtrongminhtuan(a)gmail.com> wrote:
> I have this function in C
> void func1 (const int * arg1, const int arg2)

> To call the C function in Fortran, I'm not sure which interface is
> correct:

> subroutine func1 (arg1, arg2)
> integer(c_int), intent(IN) :: arg1
> integer(c_int), intent(IN), value :: arg2
> end

> or

> subroutine func1 (arg1, arg2)
> integer(c_int), intent(IN) :: arg1
> integer(c_int), value :: arg2
> end

As I understand const, it restricts what the callee will do
with the specified variable. There is no requirement that
the caller supply an appropriately const value, but it is
allowed to do so.

That said, there is also the difference between const int *
and int * const, and, for that matter, const int * const.

For a non-pointer actual argument, as you show, intent(in)
means that the variable itself won't be modified by the called
routine. That is, as you say, int const *.

For a pointer argument, intent applies to the pointer,
not to the pointee. That is, like int * const.

Note that you could also use

type(c_ptr),value :: arg1

In which case the actual argument would be c_loc() of the
appropriate array.

> And the final one is how about this situation: const void*
> and const void**

Fortran doesn't usually use void**, though it should be
possible to have an array of type(c_ptr).

In that case, intent(in) would correspond to

void**const, and not void *const* or const void**.

-- glen