(转载)关于void *指针的一点心得....

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include <string.h>
 4 #include <unistd.h>
 5 #include <semaphore.h>
 6 #include <sys/types.h>
 7 #include <dirent.h>
 8 #include <pthread.h>
 9 #include <errno.h>
10 #include <signal.h>
11 #include <time.h>
12 
13 struct food
14 {
15     int a;
16     int b;
17     int c;
18 };
19 struct food apple;
20 
21 void* task1(void* arg)
22 {
23     apple.a = 23;
24     apple.b = 82;
25     apple.c = 59;
26     pthread_exit((void*)&apple);
27 }
28 
29 int main(int argc, char *argv[])
30 {
31     pthread_t thrd1, thrd2, thrd3;
32     void* tret;
33 
34     pthread_create(&thrd1, NULL, (void*)task1, NULL);
35     pthread_join(thrd1, (void*)&tret);
36 
37     printf("The food:%d %d %d\n", tret->a, tret->b, tret->c);  // 这行出现了严重的错误
38     printf("Main thread exit...\n");
39 
40     return 0;
41 }

程序输出:

[root@robot ~]# gcc thread_exit.c -lpthread -o thread_exit
thread_exit.c: In function 'main':
thread_exit.c:37: warning: dereferencing 'void *' pointer
thread_exit.c:37: error: request for member 'a' in something not a structure or union
thread_exit.c:37: warning: dereferencing 'void *' pointer
thread_exit.c:37: error: request for member 'b' in something not a structure or union
thread_exit.c:37: warning: dereferencing 'void *' pointer
thread_exit.c:37: error: request for member 'c' in something not a structure or union
[root@robot ~]#

原文地址:https://www.cnblogs.com/Robotke1/p/3053544.html