8.59 C_F_STRPOINTER — Convert C string into Fortran string pointer

Synopsis:
CALL C_F_STRPOINTER(CSTRARRAY, FSTRPTR[, NCHARS])
CALL C_F_STRPOINTER(CSTRPTR, FSTRPTR, NCHARS)
Description:

C_F_STRPOINTER(CSTRARRAY, FSTRPTR[, NCHARS]) pointer-associates the deferred-length character pointer FSTRPTR with the initial substring of the simply contiguous Fortran character array STRARRAY, up to the first null character, the length NCHARS if specified, or the actual size of CSTRARRAY.

CALL C_F_STRPOINTER(CSTRPTR, FSTRPTR, NCHARS) pointer-associates the deferred-length array pointer FSTRPTR with the initial substring of the continguous array of characters pointed to by the C pointer CSTRPTR, up to the first null character or length NCHARS.

Class:

Subroutine

Arguments:
CSTRARRAYRank-one character array of kind C_CHAR and character length one, which must be simply contiguous and have the TARGET attribute. It is INTENT(IN).
CSTRPTRScalar of the type C_PTR. It is INTENT(IN).
FSTRPTRScalar deferred-length character pointer of kind C_CHAR. It is INTENT(OUT).
NCHARS(Optional) Integer scalar. It is INTENT(IN). This argument can only be omitted for the CSTRARRAY form of the intrinsic, and only if STRARRAY is not assumed-size.
Example:
program main

  use iso_c_binding
  implicit none

  character (kind=c_char, len=1), dimension(15), target :: a
  type(c_ptr) :: p
  character (len=:, kind=c_char), pointer :: fp1, fp2

  a = [ character :: 'h', 'e', 'l', 'l', 'o', ' ', &
                     'w', 'o', 'r', 'l', 'd', '!', &
                     ' ', ' ', ' ']
  ! give array a terminating null so its C string length is 12.
  a(13) = C_NULL_CHAR

  ! p is a C pointer to the the first character in the array
  p = C_LOC (a(1))

  ! Make both fp1 and fp2 point to a with Fortran string length 12.
  call c_f_strpointer (p, fp1, 15)
  call c_f_strpointer (a, fp2)
end program main

Standard:

Fortran 2023 and later.

See also:

C_LOC — Obtain the C address of an object,
C_F_POINTER — Convert C into Fortran pointer,
C_F_PROCPOINTER — Convert C into Fortran procedure pointer,
F_C_STRING — Convert Fortran character scalar to C string