我尝试使用多线程做一个项目,但我对 void *
及其使用方法不是很熟悉。
我在这个函数中遇到了问题:
void *find_way_out(void *tree)
{
int i;
t_tree *tmp;
t_tree **road;
int j;
int th_nbr;
th_nbr = tree->thread_nbr;
j = 0;
road = xmalloc(sizeof(t_tree));
i = 0;
tmp = (t_tree *)tree;
road[j] = tmp;
tmp->visited[th_nbr] = 1;
while (1)
{
while (tmp->leaf[i] != NULL)
{
printf("room : %s && room next %s\n", tmp->room, tmp->leaf[i]->room);
if (tmp->leaf[i]->visited[th_nbr] == 0)
{
road[++j] = tmp;
tmp = tmp->leaf[i];
tmp->visited[th_nbr] = 1;
i = -1;
printf("going to room-> %s\n", tmp->room);
}
if (tmp->type == END)
{
printf("find end...\n");
pthread_exit(&j);
}
i++;
}
tmp = road[j];
if (--j == -1)
pthread_exit(0);
i = 0;
printf("backing to room %s\n", tmp->room);
}
pthread_exit(0);
}
错误在行:
th_nbr = tree->thread_nbr;
thread_nbr
在我的结构树中是一个整数。
编译时出现这些错误:
sources/find_way.c:21:16: warning: dereferencing ‘void *’ pointer
th_nbr = tree->thread_nbr;
^
sources/find_way.c:21:16: error: request for member ‘thread_nbr’ in something not a structure or union
你有修复它的想法吗? 谢谢。
最佳答案
在你的例子中,在取消引用时,
th_nbr = tree->thread_nbr;
tree
是 void *
类型。
你需要移动
tmp = (t_tree *)tree;
之前
th_nbr = tmp->thread_nbr; //tree changed to tmp
因此,在解除引用时,tmp
应该是 tree
类型的指针。
关于c - 取消引用 ‘void *’ 指针和转换不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29852752/