结构体程序错误
i请问下我程序为什么会报如下错误啊,应如何改正啊,谢谢指教!
threadexstatus.c: In function ‘thfn4’:
threadexstatus.c:56:4: error: request for member ‘b’ in something not a structure or union
threadexstatus.c:57:4: error: request for member ‘c’ in something not a structure or union
threadexstatus.c:82:75: error: expected expression before ‘)’ token
threadexstatus.c:115:71: error: expected ‘{’ before ‘*’ token
threadexstatus.c:115:77: error: request for member ‘c’ in something not a structure or union
threadexstatus.c:131:52: error: expected ‘)’ before ‘res’
threadexstatus.c:131:57: error: expected ‘)’ before ‘->’ token
1 #include <unistd.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <pthread.h>
5
6 struct a
7 {
8 int b;
9 int c;
10 };
11
12 void * thfn1(void * arg)
13 {
14 struct a r1;
15
16 printf("the first\n");
17
18 r1.b = 10;
19 r1.c = 11;
20
21 return (void *)(&r1);
22 }
23
24 void * thfn2(void * arg)
25 {
26 struct a * r2;
27
28 printf("the second one \n");
29
30 r2 = (struct a *)malloc(sizeof(struct a));
31
32 r2 -> b = 10;
33 r2 -> c = 11;
34
35 return (void *)(&r2);
36 }
37
38 void * thfn3(void * arg)
39 {
40 struct a r3;
41
42 printf("the third one\n");
43
44 r3.b = 10;
45 r3.c = 11;
46
47 return (void *)(&r3);
48 }
49
50 void * thfn4(void * arg)
51 {
52 struct a * r4 = (struct a * )arg;
53
54 printf("the fourth one\n");
55
56 r4.b = 10;
57 r4.c = 11;
58
59 return (void *)(&r4);
60 }
61
62 int main(void)
63 {
64 int err;
65 void * res;
66 pthread_t tid1, tid2, tid3, tid4;
67
68 err = pthread_create(&tid1, NULL, thfn1, NULL);
69 if(err != 0)
70 {
71 printf("can not create thread %s\n", strerror(err));
72 exit(1);
73 }
74
75 err = pthread_join(tid1, &res);
76 if(err != 0)
77 {
78 printf("can not create thread %s\n", strerror(err));
79 exit(1);
80 }
81
82 printf("result from thfn1: %d, %d\n", ((struct a *)res)->b, ((struct a *))res->c);
83
84 err = pthread_create(&tid2, NULL, thfn2, NULL);
85 if(err != 0)
86 {
87 printf("can not create thread %s\n", strerror(err));
88 exit(1);
89 }
90
91 err = pthread_join(tid2, &res);
92 if(err != 0)
93 {
94 printf("can not create thread %s\n", strerror(err));
95 exit(1);
96 }
97
98 printf("result from thfn2: %d, %d\n", ((struct a *)res)->b, ((struct a *)res)->c);
99 free(res);
100
101 err = pthread_create(&tid3, NULL, thfn3, NULL);
102 if(err != 0)
103 {
104 printf("can not create thread %s\n", strerror(err));
105 exit(1);
106 }
107
108 err = pthread_join(tid3, &res);
109 if(err != 0)
110 {
111 printf("can not create thread %s\n", strerror(err));
112 exit(1);
113 }
114
115 printf("result from thfn3: %d, %d\n", ((struct a *)res)->b, ((struct *)res)->c);
116
117 err = pthread_create(&tid4, NULL, thfn4, NULL);
118 if(err != 0)
119 {
120 printf("can not create thread %s\n", strerror(err));
121 exit(1);
122 }
123
124 err = pthread_join(tid4, &res);
125 if(err != 0)
126 {
127 printf("can not create thread %s\n", strerror(err));
128 exit(1);
129 }
130
131 printf("result from thfn4: %d, %d\n", (struct a *(res))->b, (struct a *(res))->c);
132
133 return 0;
134 }
65,1-4 28%