I have the following function call in C++:
int strLength = 20;
char* name;
getName(name, strLength);
printf(\"name: %s\\n\", name);
and i
The problem was in assigning a character array of rank n to a character scalar of length n, which was addressed in this question. Changing the Fortran routine as shown below solved the issue.
Since I'm working in a legacy system I had to go for the solution indicated below. Obviously it's not exactly the same, but it still shows the general structure. If anyone else should go for this solution as well you should probably follow the recommendations in Matt P's comment.
Although this is the solution I went for, I feel like the answer from Matt P is a better solution to the general problem as stated in the question title, which is why I accepted that as the answer.
C++
int strLength = 20;
char* name = new char[strLength];
getName(name, strLength);
printf("name: %s\n", name);
Fortran
subroutine getName(name) bind (c, name='GETNAME')
use,intrinsic :: iso_c_binding
implicit none
character(c_char),dimension(20), intent(out) :: name
character(20) :: fName
fName = 'Martin'//c_null_char
do j=1,len(fName )
name(j) = fName (j:j)
enddo
end subroutine getName