gpt4 book ai didi

c - 难以通过 pthread_create 传递结构

转载 作者:行者123 更新时间:2023-12-02 07:07:07 24 4
gpt4 key购买 nike

我有一个结构数组,我打算将数组的每个元素传递到 for 循环中的单独 pthread。

这是我的结构:

struct arrayData{
int *a;
int *b;
int up, low;
}

这是指向第一个结构和 malloc 的指针(不知道我是否完全明白这里发生了什么):

struct arrayData * instance;
instance = malloc(sizeof(struct arrayData)*n);

这是我对 pthread_create 的调用:

pthread_create( &thread[i], NULL, add, (void *)instance[i]);

对于该行,我收到消息“无法转换为指针类型”。

那行可能有什么问题?

最佳答案

您正在尝试将结构转换为最后一个参数中的指针。您需要使用 & 传递结构的地址。

pthread_create( &thread[i], NULL, add, &instance[i]);

作为约根森 mentioned , void * 转换是不必要的。

关于c - 难以通过 pthread_create 传递结构,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9914049/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com