I\'m writing code that will call a C function from Fortran using Fortran\'s C interoperability mechanism (introduced in Fortran 2003 and implemented in newer versions of gfo
The way we do it is to use a C_PTR
array to point to strings. For example:
CHARACTER(LEN=100), DIMENSION(numStrings), TARGET :: stringArray
TYPE(C_PTR), DIMENSION(numStrings) :: stringPtrs
then we set our strings in stringArray
, remembering to null-terminate them such as:
DO ns = 1, numStrings
stringArray(ns) = "My String"//C_NULL_CHAR
stringPtrs(ns) = C_LOC(stringArray(ns))
END DO
and pass stringPtrs
to the C function.
The C function has the interface:
void stringFunc(int *numStrings, char **stringArray) {
int i;
for(i=0;i<*numStrings;++i) {
printf("%s\n",stringArray[i]);
}
}