# f_subptr_str

Return a borrowed rank-one `character(len=1)` view of a persistent string
or contiguous character array, with every count and offset in **characters**.

```fortran
use futile                                  ! or:
use f_subptr_module, only: f_subptr_str
```

## Signature

Generated from the FYPP documentation stub `doc/generated/f_subptr_doc.f90`
(emitted by `fypp/subptr-doc.fypp` from the same loops as `flib/f_subptr.f90`).

### *interface*  f_subptr_module/f_subptr_str(ptr, [region, from, size, lbound, n, shape])

Character-unit windows; scalar and array capacities are both known.
Same selectors as f_subptr, but every count/offset uses characters.

* **Parameters:**
  **ptr** *[**character(len=\*)* *,* *target,rank 0-7* *]* :: Persistent scalar string or contiguous character array, rank 0–7; its capacity is always known.
* **Options:**
  * **region** *[*[*array_bounds*](../../memory.md#f/module_f_malloc/array_bounds) *,* *in* *]* :: Inclusive first/last character offsets, built with `first.to.last`; reversed endpoints give an explicit empty window. Excludes `from`.
  * **from** *[**integer(f_integer|f_size_t)* *,* *in* *]* :: One-based character offset in the flattened source (default 1). Kinds: `f_integer`, `f_size_t`.
  * **size** *[**integer(f_integer|f_size_t)* *,* *in* *]* :: Legacy spelling of `n`; do not supply both.
  * **lbound** *[**integer(f_integer|f_size_t)* *,* *in* *]* :: Lower bound of the rank-one result (default 1); does not change the selection.
  * **n** *[**integer(f_integer|f_size_t)* *,* *in* *]* :: Nonnegative number of selected characters. Without any selector the whole source, or the suffix at `from`, is selected.
  * **shape** (\*) *[**integer(f_integer|f_size_t)* *,* *in* *]* :: Zero to seven nonnegative extents whose checked product is the character count; the result still has rank one.

## Typical calls

```fortran
characters => f_subptr_str(text)
characters => f_subptr_str(strings, from=offset, n=count)
```

The window controls have the defaults and exclusivity rules of
[f_subptr](f_subptr.md), but count characters, not string elements. There is no
`ptr_addr` spelling: the capacity of a character source is always known
from its length and array size, including for scalar strings.

## Returns

A `character(len=1)` pointer with one element per storage character, in
Fortran array-element order. For `character(len=3) :: strings(2)`,
`f_subptr(strings)` preserves two length-three elements, while
`f_subptr_str(strings)` exposes six length-one elements. Trailing blanks and
embedded NULs are included; no separators or terminators are inserted.

## Supported ranks

**128** specific procedures implement `f_subptr_str` (generated by FYPP; table emitted by the documentation stub `f_subptr_doc.f90` from the same loops).

| Type (kind)          | ranks of `ptr`   | count kinds             |   specifics |
|----------------------|------------------|-------------------------|-------------|
| `character` (len=\*) | 0-7              | `f_integer`, `f_size_t` |         128 |

## Notes

### Ownership

Writes through the view modify the original string storage. The view neither
allocates nor owns payload: keep the owner alive and nullify the view before
freeing the owner.

### Empty windows

An explicit zero character window returns null. An implicit zero-character
payload (a zero-length scalar, or an array of zero-length strings) returns an
associated empty view backed by a module-owned zero-element target; no
owner-address identity is promised in that case. As for [f_subptr](f_subptr.md), never
bounds-remap a null result.

### Errors

Windows exceeding the known capacity, invalid selectors, overflow and
noncontiguous nonempty storage raise `ERR_INVALID_MALLOC`; a caught error
returns null.

### C interoperability

Where default character kind equals `c_char`, a nonempty view’s `c_loc`
can be passed to C. The API does not append NUL, trim blanks, encode text or
scan for a terminator; units are storage characters, not Unicode code points.
Supply a NUL inside the selected window before calling an unbounded C routine
such as `strlen`. An empty view supplies no C string address.

## Examples

Compiled and run by CI (`tests/flib/character_buffers_check.F90`): shared
addresses, C `strlen` and C-side mutation through the view.

```fortran
  text='AB '//c_null_char//'tail'
  chars=>f_subptr_str(text)
  if(len(chars)/=1.or.f_size(chars)/=8)error stop 'flat scalar string view'
  if(.not.c_associated(c_loc(chars(1)),c_loc(text)))error stop 'borrowed C address'
  ! The caller supplied the NUL above, inside the selected view.
  if(character_buffer_strlen(c_loc(chars(1)))/=3_c_size_t)error stop 'C strlen'
  call character_buffer_set(c_loc(chars(1)),1_c_size_t,'Z')
  if(text/='AZ '//c_null_char//'tail')error stop 'C mutation or trailing canary'
  nullify(chars)
```

## See also

* [f_subptr](f_subptr.md) – the same windows counted in elements, for any intrinsic type.
* [f_memcpy](f_memcpy.md) – copy raw storage into or out of a character view.
* [f_dump_buffer](f_dump_buffer.md) – binding-owned character storage, copied rather than borrowed.
* `f_atoi` / `f_itoa` – explicit ASCII conversions instead of raw copies ([Memory handling](../../memory.md)).
