File tree Expand file tree Collapse file tree 3 files changed +27
-5
lines changed
Expand file tree Collapse file tree 3 files changed +27
-5
lines changed Original file line number Diff line number Diff line change @@ -547,8 +547,8 @@ The result is an `allocatable` length `character` scalar with up to `128` cached
547547
548548#### Description
549549
550- Convert a Fortran character string to a C character array.
551- This function converts a Fortran string into a C-style string , ensuring proper null-termination for use in C functions or libraries.
550+ Convert a Fortran ` character ` string or a ` type(string_type) ` variable to a C character array.
551+ This function converts a Fortran string into a C-style array of characters , ensuring proper null-termination for use in C functions or libraries.
552552
553553#### Syntax
554554
@@ -564,8 +564,7 @@ Pure function.
564564
565565#### Argument
566566
567- - ` value ` : Shall be a ` character(len=*) ` string.
568- This is an ` intent(in) ` argument.
567+ - ` value ` : Shall be a ` character(len=*) ` string or a ` type(string_type) ` variable. It is an ` intent(in) ` argument.
569568 The Fortran string that will be converted to a C character array.
570569
571570#### Result value
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ ADD_EXAMPLE(slice)
99ADD_EXAMPLE(starts_with)
1010ADD_EXAMPLE(strip)
1111ADD_EXAMPLE(to_string)
12+ ADD_EXAMPLE(to_c_string)
1213ADD_EXAMPLE(zfill)
1314ADD_EXAMPLE(string_to_number)
14- ADD_EXAMPLE(stream_of_strings_to_numbers)
15+ ADD_EXAMPLE(stream_of_strings_to_numbers)
Original file line number Diff line number Diff line change 1+ program example_to_c_string
2+ use stdlib_strings, only: to_c_string
3+ use stdlib_string_type, only: string_type
4+ use stdlib_kinds, only: c_char
5+ implicit none
6+
7+ character (kind= c_char), allocatable :: cstr(:),cstr2(:)
8+ character (* ), parameter :: hello = " Hello, World!"
9+
10+ ! Convert character array
11+ cstr = to_c_string(hello)
12+
13+ ! Convert string type
14+ cstr2 = to_c_string(string_type(hello))
15+
16+ if (size (cstr)==size (cstr2) .and. all (cstr== cstr2)) then
17+ stop 0
18+ else
19+ error stop ' String conversion error'
20+ end if
21+
22+ end program example_to_c_string
You can’t perform that action at this time.
0 commit comments