Search code examples
arrayscmultithreadingtypesstructure

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]));

There is an error message: 'Incompatible pointer types passing 'void *(struct thread_args )' to the parameter of type 'void * _Nullable ( _Nonnull)(void * _Nullable)''

How can I fix it?


Solution

  • The thread function passed to pthread_create needs to take a void * argument.

    If you need the argument to be of another type, then define a local variable and initialize with casting inside the function instead:

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