C Library Functions  - s2c (3)

NAME

s2c(3f) - [M_strings:ARRAY] convert character variable to array of characters with last element set to null (LICENSE:PD)

CONTENTS

Synopsis
Description
Examples
Author
License

SYNOPSIS

function s2c(string)

    character(len=*),intent=(in)  :: string
    character(len=1),allocatable  :: s2c(:)

DESCRIPTION

Given a character variable convert it to an array of single-character character variables with the last element set to a null character. This is generally used to pass character variables to C procedures.

EXAMPLES

character(len=3),allocatable :: array(:)
integer
  :: i ! put one character into each 3-character element of array array = [(string(i:i),i=1,len(string))] ! write array with ASCII Decimal Equivalent below it except show ! unprintable characters like NULL as "XXX" write(*,g) merge(’XXX’,array,iachar(array(:)(1:1)) < 32) write(*,g) iachar(array(:)(1:1))

Sample Program:

    program demo_s2c
    use M_strings, only : s2c
    implicit none
    character(len=*),parameter   :: string="single string"
    character(len=*),parameter   :: g= ’(1x,*("[",g3.3,"]":))’
    character(len=3),allocatable :: array(:)
       write(*,*)’INPUT STRING ’,trim(string)
       ! put one character into each 3-character element of array
       array=s2c(string)
       ! write array with ASCII Decimal Equivalent below it except show
       ! unprintable characters like NULL as "XXX"
       write(*,g) merge(’XXX’,array,iachar(array(:)(1:1)) < 32)
       write(*,g) iachar(array(:)(1:1))
    end program demo_s2c

Expected output:

   INPUT STRING single string
   [s  ][i  ][n  ][g  ][l  ][e  ][   ][s  ][t  ][r  ][i  ][n  ][g  ][XXX]
   [115][105][110][103][108][101][ 32][115][116][114][105][110][103][  0]

AUTHOR

John S. Urban

LICENSE

Public Domain


Nemo Release 3.1 s2c (3) July 22, 2023
Generated by manServer 1.08 from 49696264-b1b5-46d6-9b80-291ef84b21f6 using man macros.