c - 取消引用进入函数然后传递给另一个函数的双指针
问题描述
我在执行以下场景时遇到问题。我有一个功能:
void testing(mystruct str**) {
pthread threads[5];
for(int i = 0; i < 5; i++) {
pthread_create(&thread[i], NULL, process, (void)&(*str)); //not really sure how to pass this here
}
}
void * process(void *arg) {
struct mystruct **value = arg; //probably wrong but it compiles, then blows up some where else because its not really the value that was original passed.
}
我是 C 新手,据我所知,这是它在做什么,
- 我得到一个双指针
将它传递给函数我会将它转换为 void 因为这是函数接受的内容以及 str 指针的地址(或者它是指向指针的指针的地址?&str?)*然后在获取它时另一方面,我设置了它,我尝试将它转换为结构,但这也不是很顺利:
struct mystruct **value = *(mystruct *)arg;
但我得到“取消引用指向不完整类型的指针”
任何帮助理解这一点将不胜感激。
解决方案
您应该能够按原样传递指针,因为 C 中的每个对象指针类型都可以隐式转换为/从void*
. 尽管如此,将指针取消引用一级更正确。那是:
pthread_create(&thread[i], NULL, process, *str);
重要提示:这假定str
指向的变量在线程执行时不会超出范围!将指向局部变量的指针传递给线程是常见的错误来源。
从那里开始,您的线程回调应该执行以下操作:
void * process(void *arg) {
struct mystruct* ms = arg;
struct mystruct** ptr_ms = &arg;
但是,错误“取消引用指向不完整类型的指针”仅仅意味着结构定义对线程不可见,导致结构类型被视为不完整。所以这个问题也可能是缺失的#include
。
推荐阅读
- c++ - Boost Spirit X3解析语法a^(2n+1)
- whatsapp - whatsapp-web api(pedroslopez)触发词不起作用
- python - 获取卷积算法失败。这可能是因为 cuDNN 未能初始化 Flask 框架
- python - 为什么 PyCharm 在字符串后面加空格?
- reactjs - webpack5 不从 .json 文件导入内容?
- python - 如何创建 PLY yacc 读取数组?
- python-3.x - Why does merging unequally matched rows not work on local dataset?
- swiftui - SwiftUI .swipeactions 和核心数据
- python - 如何在while true中添加条件
- google-sheets - 如何在谷歌查询中的选择语句中获取月份数