结构体程序错误

liyi54188 2011-08-07 10:07:49
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%
...全文
92 8 打赏 收藏 转发到动态 举报
写回复
用AI写文章
8 条回复
切换为时间正序
请发表友善的回复…
发表回复
liyi54188 2011-08-08
  • 打赏
  • 举报
回复
6楼的问题我已经改过来了,7楼那样改不行阿,还是有那个错误
dongjiawei316 2011-08-08
  • 打赏
  • 举报
回复

typedef struct
{
int b;
int c;
}r3,a;

yong_f 2011-08-08
  • 打赏
  • 举报
回复
56 r4.b = 10;
57 r4.c = 11;
r4为指针
应改为
56 r4->b = 10;
57 r4->c = 11;
82 printf("result from thfn1: %d, %d\n", (struct a *)res->b, (struct a *)res->c);
dongjiawei316 2011-08-08
  • 打赏
  • 举报
回复
哦!你这样改一下结构体定义。

typedef struct
{
int b;
int c;
}a,r3;


liyi54188 2011-08-08
  • 打赏
  • 举报
回复
这样改不行阿,报错
threadexstatus.c:130:41: error: ‘a’ undeclared (first use in this function)
threadexstatus.c:130:41: note: each undeclared identifier is reported only once for each function it appears in
我56行改了,
6 struct a
7 {
8 int b;
9 int c;
10 }r3;
结构体是这样定义的
dongjiawei316 2011-08-07
  • 打赏
  • 举报
回复
115行 ((struct a *)res)->b 改为 ((a *)res)->

同理131行
dongjiawei316 2011-08-07
  • 打赏
  • 举报
回复
56行 把.换成->
bicener 2011-08-07
  • 打赏
  • 举报
回复
你的r4就指针变量嘛,当然要用->来访问啦!

23,120

社区成员

发帖
与我相关
我的任务
社区描述
Linux/Unix社区 应用程序开发区
社区管理员
  • 应用程序开发区社区
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧