简体   繁体   English

将“void *(struct thread_args *)”传递给“void * _Nullable (* _Nonnull)(void * _Nullable)”类型参数的不兼容指针类型

[英]Incompatible pointer types passing 'void *(struct thread_args *)' to parameter of type 'void * _Nullable (* _Nonnull)(void * _Nullable)'

struct thread_args
{
    int a;
    int b;
    int c;
};

void* check_subgrid(struct thread_args *p)
{
    int x = p->a;
    int y = p->b;
    int z = p->c;

    for (int i = x; i < x + 3; i++) {
        int check[9] = {};
        for (int j = y; j < y + 3; j++) {
            if ( check[ sudoku[i][j] - 1 ] == 0 ) {
                check[ sudoku[i][j] - 1 ] = 1;
            }
            else {
                valid[2][z] = -1;
                break;
            }
        }
    }
    if( valid[2][z] == -1 ) valid[2][z] = 0;
    else valid[2][z] = 1;
    return 0;
}

void check_sudoku(void)
{
    pthread_t p_thread[11];
    int thr_id , result;

    struct thread_args p[9];
    
    p[0].a = 0;
    p[0].b = 0;
    p[0].c = 0;
    
    thr_id = pthread_create(&p_thread[2], NULL, check_subgrid, (void*)&(p[0]));
    if (thr_id < 0)
    {
        perror("thread create error : ");
        exit(0);
    }
}

When I use pthread_create() function with 'Structures array p[]' as arguments, like thr_id = pthread_create(&p_thread[2], NULL, check_subgrid, (void*)&(p[0])); When I use pthread_create() function with 'Structures array p[]' as arguments, like thr_id = pthread_create(&p_thread[2], NULL, check_subgrid, (void*)&(p[0]));

There is an error message: 'Incompatible pointer types passing 'void *(struct thread_args )' to the parameter of type 'void * _Nullable ( _Nonnull)(void * _Nullable)''有一条错误消息:'不兼容的指针类型将'void *(struct thread_args )'传递给'void * _Nullable(_Nonnull)(void * _Nullable)'类型的参数

How can I fix it?我该如何解决?

The thread function passed to pthread_create needs to take a void * argument.传递给pthread_create的线程 function 需要采用void *参数。

If you need the argument to be of another type, then define a local variable and initialize with casting inside the function instead:如果您需要参数是另一种类型,则定义一个局部变量并使用 function 内部的转换进行初始化:

void* check_subgrid(void *ap)
{
    struct thread_args *p = (struct thread_args *) ap;

    // ...
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM