首页 > 解决方案 > 取消引用进入函数然后传递给另一个函数的双指针

问题描述

我在执行以下场景时遇到问题。我有一个功能:

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 新手,据我所知,这是它在做什么,

但我得到“取消引用指向不完整类型的指针”

任何帮助理解这一点将不胜感激。

标签: cpointers

解决方案


您应该能够按原样传递指针,因为 C 中的每个对象指针类型都可以隐式转换为/从void*. 尽管如此,将指针取消引用一级更正确。那是:

pthread_create(&thread[i], NULL, process, *str);

重要提示:这假定str指向的变量在线程执行时不会超出范围!将指向局部变量的指针传递给线程是常见的错误来源。

从那里开始,您的线程回调应该执行以下操作:

void * process(void *arg) {
    struct mystruct* ms = arg; 
    struct mystruct** ptr_ms = &arg; 

但是,错误“取消引用指向不完整类型的指针”仅仅意味着结构定义对线程不可见,导致结构类型被视为不完整。所以这个问题也可能是缺失的#include


推荐阅读