您的位置:首页 > 其它

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

2013-05-01 20:26 295 查看
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <semaphore.h>
#include <sys/types.h>
#include <dirent.h>
#include <pthread.h>
#include <errno.h>
#include <signal.h>
#include <time.h>

struct food
{
int a;
int b;
int c;
};
struct food apple;

void* task1(void* arg)
{
apple.a = 23;
apple.b = 82;
apple.c = 59;
pthread_exit((void*)&apple);
}

int main(int argc, char *argv[])
{
pthread_t thrd1, thrd2, thrd3;
void* tret;

pthread_create(&thrd1, NULL, (void*)task1, NULL);
pthread_join(thrd1, (void*)&tret);

printf("The food:%d %d %d\n", tret->a, tret->b, tret->c);  // 这行出现了严重的错误
printf("Main thread exit...\n");

return 0;
}


程序输出:

[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 ~]#


内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: