Skip to content

Commit f687ed5

Browse files
committed
less cumbersome string approach for struct
1 parent edb482f commit f687ed5

File tree

6 files changed

+11
-28
lines changed

6 files changed

+11
-28
lines changed

src/c/struct_lib.c

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff 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
}

src/c/struct_main.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,13 @@
22
#include <string.h>
33
#include <stdlib.h>
44

5+
enum { Lchar = 1000 };
6+
57
struct 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

1314
extern void struct_check(struct params *);
@@ -19,7 +20,6 @@ struct params s;
1920
s.my_int = 123;
2021
s.my_bool = true;
2122
strcpy(s.my_char, "Hello");
22-
s.Lmy_char = strlen(s.my_char);
2323

2424
struct_check(&s);
2525

src/cxx/struct_lib.cxx

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

1514
void 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
}

src/cxx/struct_main.cxx

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff 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;
1817
s.my_int = 123;
1918
s.my_bool = true;
2019
strcpy(s.my_char, "Hello");
21-
s.Lmy_char = strlen(s.my_char);
2220

2321
struct_check(&s);
2422

src/fortran/struct_lib.f90

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ module struct_rx
99
!! order and length must match in Fortran and C
1010
integer(c_int) :: my_int
1111
logical(c_bool) :: my_bool
12-
integer(c_int) :: Lmy_char
1312
character(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

2928
if(s%my_int /= 123) error stop "my_int /= 123"
3029
if(.not. s%my_bool) error stop "my_bool /= .true."
31-
if(s%Lmy_char /= 5) error stop "Lmy_char /= 5"
3230

3331
block
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)
4341
end block
4442

4543
if(my_char /= "Hello") error stop "my_char /= 'Hello'"

src/fortran/struct_main.f90

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ program struct_tx
99
!! order and length must match in Fortran and C
1010
integer(c_int) :: my_int
1111
logical(c_bool) :: my_bool
12-
integer(c_int) :: Lmy_char
1312
character(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

3534
s%my_int = 123
3635
s%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)
4139
end do
4240
s%my_char(i+1) = c_null_char
4341

42+
call struct_check(s)
4443

4544
print *, "OK: Fortran => C struct"
4645
end program

0 commit comments

Comments
 (0)