Skip to content

Commit e7d9b58

Browse files
committed
fix struct to use pointer
1 parent 8da7d98 commit e7d9b58

File tree

5 files changed

+19
-15
lines changed

5 files changed

+19
-15
lines changed

src/c/my_struct.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ struct params {
1111
char my_char[LMAX];
1212
};
1313

14-
void struct_check(struct params);
14+
void struct_check(struct params *);
1515

1616
#ifdef __cplusplus
1717
}

src/c/struct_lib.c

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,20 +6,22 @@
66
#include "my_struct.h"
77

88

9-
void struct_check(struct params s) {
9+
void struct_check(struct params *s) {
1010

11-
if (s.my_int != 123) {
12-
fprintf(stderr, "Error: my_int = %d\n", s.my_int);
11+
struct params *p = s;
12+
13+
if (p->my_int != 123) {
14+
fprintf(stderr, "Error: my_int = %d\n", p->my_int);
1315
exit(EXIT_FAILURE);
1416
}
1517

16-
if (! s.my_bool) {
18+
if (! p->my_bool) {
1719
fprintf(stderr, "Error: my_bool is false\n");
1820
exit(EXIT_FAILURE);
1921
}
2022

21-
if (strncmp(s.my_char, "Hello", 5) != 0) {
22-
fprintf(stderr, "Error: my_char != Hello: %s\n", s.my_char);
23+
if (strncmp(p->my_char, "Hello", 5) != 0) {
24+
fprintf(stderr, "Error: my_char != Hello: %s\n", p->my_char);
2325
exit(EXIT_FAILURE);
2426
}
2527

src/c/struct_main.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ s.my_int = 123;
1212
s.my_bool = true;
1313
strcpy(s.my_char, "Hello");
1414

15-
struct_check(s);
15+
struct_check(&s);
1616

1717
return EXIT_SUCCESS;
1818

src/cxx/struct_lib.cxx

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,22 @@
44

55
#include "my_struct.h"
66

7-
void struct_check(struct params s) {
7+
void struct_check(struct params *s) {
88

9-
if (s.my_int != 123) {
10-
std::cerr << "Error: my_int = " << s.my_int << std::endl;
9+
struct params *p = s;
10+
11+
if (p->my_int != 123) {
12+
std::cerr << "Error: my_int = " << p->my_int << std::endl;
1113
exit(EXIT_FAILURE);
1214
}
1315

14-
if (! s.my_bool) {
16+
if (! p->my_bool) {
1517
std::cerr << "Error: my_bool is false" << std::endl;
1618
exit(EXIT_FAILURE);
1719
}
1820

19-
if (strncmp(s.my_char, "Hello", 5) != 0) {
20-
std::cerr << "Error: my_char != Hello " << s.my_char << std::endl;
21+
if (strncmp(p->my_char, "Hello", 5) != 0) {
22+
std::cerr << "Error: my_char != Hello " << p->my_char << std::endl;
2123
exit(EXIT_FAILURE);
2224
}
2325

src/cxx/struct_main.cxx

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ s.my_int = 123;
1111
s.my_bool = true;
1212
strcpy(s.my_char, "Hello");
1313

14-
struct_check(s);
14+
struct_check(&s);
1515

1616
return EXIT_SUCCESS;
1717

0 commit comments

Comments
 (0)