File tree Expand file tree Collapse file tree 6 files changed +11
-28
lines changed
Expand file tree Collapse file tree 6 files changed +11
-28
lines changed Original file line number Diff line number Diff line change @@ -7,7 +7,6 @@ struct params {
77 // order and lengths must match in Fortran and C
88 int my_int ;
99 bool my_bool ;
10- int Lmy_char ;
1110 char my_char [1000 ];
1211};
1312
@@ -25,12 +24,7 @@ if (! s.my_bool) {
2524 exit (EXIT_FAILURE );
2625}
2726
28- if (s .Lmy_char != 5 ) {
29- fprintf (stderr , "Error: my_char wrong length %d\n" , s .Lmy_char );
30- exit (EXIT_FAILURE );
31- }
32-
33- if (strcmp (s .my_char , "Hello" ) != 0 ) {
27+ if (strncmp (s .my_char , "Hello" , 5 ) != 0 ) {
3428 fprintf (stderr , "Error: my_char != Hello: %s\n" , s .my_char );
3529 exit (EXIT_FAILURE );
3630}
Original file line number Diff line number Diff line change 22#include <string.h>
33#include <stdlib.h>
44
5+ enum { Lchar = 1000 };
6+
57struct params {
68 // order and lengths must match in Fortran and C
79 int my_int ;
810 bool my_bool ;
9- int Lmy_char ;
10- char my_char [1000 ];
11+ char my_char [Lchar ];
1112};
1213
1314extern void struct_check (struct params * );
@@ -19,7 +20,6 @@ struct params s;
1920s .my_int = 123 ;
2021s .my_bool = true;
2122strcpy (s .my_char , "Hello" );
22- s .Lmy_char = strlen (s .my_char );
2323
2424struct_check (& s );
2525
Original file line number Diff line number Diff line change @@ -6,11 +6,10 @@ struct params {
66 // order and lengths must match in Fortran and C
77 int my_int;
88 bool my_bool;
9- int Lmy_char;
109 char my_char[1000 ];
1110};
1211
13- void struct_check (struct params );
12+ extern " C " void struct_check (struct params );
1413
1514void struct_check (struct params s) {
1615
@@ -24,12 +23,7 @@ if (! s.my_bool) {
2423 exit (EXIT_FAILURE);
2524}
2625
27- if (s.Lmy_char != 5 ) {
28- std::cerr << " Error: my_char wrong length " << s.Lmy_char << std::endl;
29- exit (EXIT_FAILURE);
30- }
31-
32- if (strcmp (s.my_char , " Hello" ) != 0 ) {
26+ if (strncmp (s.my_char , " Hello" , 5 ) != 0 ) {
3327 std::cerr << " Error: my_char != Hello " << s.my_char << std::endl;
3428 exit (EXIT_FAILURE);
3529}
Original file line number Diff line number Diff line change @@ -5,7 +5,6 @@ struct params {
55 // order and lengths must match in Fortran and C
66 int my_int;
77 bool my_bool;
8- int Lmy_char;
98 char my_char[1000 ];
109};
1110
@@ -18,7 +17,6 @@ struct params s;
1817s.my_int = 123 ;
1918s.my_bool = true ;
2019strcpy (s.my_char , " Hello" );
21- s.Lmy_char = strlen (s.my_char );
2220
2321struct_check (&s);
2422
Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ module struct_rx
99! ! order and length must match in Fortran and C
1010integer (c_int) :: my_int
1111logical (c_bool) :: my_bool
12- integer (c_int) :: Lmy_char
1312character (kind= c_char) :: my_char(1000 )
1413! ! character(kind=c_char) in bind(c) type cannot be allocatable. Just have to make it "long enough"
1514! ! or use iso_c_binding.h stuff
@@ -28,18 +27,17 @@ pure subroutine struct_check(s) bind(C)
2827
2928if (s% my_int /= 123 ) error stop " my_int /= 123"
3029if (.not. s% my_bool) error stop " my_bool /= .true."
31- if (s% Lmy_char /= 5 ) error stop " Lmy_char /= 5"
3230
3331block
34- character (s % Lmy_char ) :: buf
32+ character (size (s % my_char) ) :: buf
3533 integer :: i
3634 buf = " " ! < ensure buf has no garbage characters
3735
38- do i = 1 , s % Lmy_char
36+ do i = 1 , len (buf)
3937 if (s% my_char(i) == c_null_char) exit
4038 buf(i:i) = s% my_char(i)
4139 enddo
42- my_char = buf
40+ my_char = trim ( buf)
4341end block
4442
4543if (my_char /= " Hello" ) error stop " my_char /= 'Hello'"
Original file line number Diff line number Diff line change @@ -9,7 +9,6 @@ program struct_tx
99! ! order and length must match in Fortran and C
1010integer (c_int) :: my_int
1111logical (c_bool) :: my_bool
12- integer (c_int) :: Lmy_char
1312character (kind= c_char) :: my_char(1000 )
1413! ! character(kind=c_char) in bind(c) type cannot be allocatable. Just have to make it "long enough"
1514! ! or use iso_c_binding.h stuff
@@ -34,13 +33,13 @@ end subroutine struct_check
3433
3534s% my_int = 123
3635s% my_bool = .true.
37- s% Lmy_char = len (my_char)
3836
39- do i = 1 , s % Lmy_char
37+ do i = 1 , len (my_char)
4038 s% my_char(i) = my_char(i:i)
4139end do
4240s% my_char(i+1 ) = c_null_char
4341
42+ call struct_check(s)
4443
4544print * , " OK: Fortran => C struct"
4645end program
You can’t perform that action at this time.
0 commit comments