Skip to content

Commit f958b74

Browse files
committed
add example
1 parent 311d918 commit f958b74

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
lines changed

doc/specs/stdlib_strings.md

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff 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

example/strings/CMakeLists.txt

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ ADD_EXAMPLE(slice)
99
ADD_EXAMPLE(starts_with)
1010
ADD_EXAMPLE(strip)
1111
ADD_EXAMPLE(to_string)
12+
ADD_EXAMPLE(to_c_string)
1213
ADD_EXAMPLE(zfill)
1314
ADD_EXAMPLE(string_to_number)
14-
ADD_EXAMPLE(stream_of_strings_to_numbers)
15+
ADD_EXAMPLE(stream_of_strings_to_numbers)
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
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

0 commit comments

Comments
 (0)