Binding multiple C functions under one Fortran interface

It looks like generic procedure resolution is strict in terms of satisfying the TKR rule:

A dummy argument is type, kind, and rank compatible, or TKR compatible, with another dummy argument if the first is type compatible with the second, the kind type parameters of the first have the same values as the corresponding kind type parameters of the second, and both have the same rank or either is assumed-rank.

This rule is violated when you call the routine via the generic name form instead of the specific name form1.

Besides the solution of @FedericoPerini which creates a copy of the string, you could also do the following:

character(kind=c_char,len=40), target :: label

label = "abracadabra"//c_null_char
call form1(label)  ! sequence association

block
    character(c_char), pointer :: p_label(:) => null()

    ! Fortran len_trim() includes the termination character
    ! C strlen() does not include the termination character

    call c_f_pointer(c_loc(label),p_label,[len_trim(label)])
    call form( p_label )  ! generic resolution to form1
end block
2 Likes