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
547
547
548
548
#### Description
549
549
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.
552
552
553
553
#### Syntax
554
554
@@ -564,8 +564,7 @@ Pure function.
564
564
565
565
#### Argument
566
566
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.
569
568
The Fortran string that will be converted to a C character array.
570
569
571
570
#### Result value
Original file line number Diff line number Diff line change @@ -9,6 +9,7 @@ ADD_EXAMPLE(slice)
9
9
ADD_EXAMPLE (starts_with )
10
10
ADD_EXAMPLE (strip )
11
11
ADD_EXAMPLE (to_string )
12
+ ADD_EXAMPLE (to_c_string )
12
13
ADD_EXAMPLE (zfill )
13
14
ADD_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