不兼容的指针类型将'void(void *)传递给'void *类型的参数

时间:2019-09-24 20:48:41

标签: c pthreads

我创建了如下的pthread:

void function1(void *s) {
start = (*(int *)s ;
}

pthread_t threads[numthreads];
int ids[numthreads];
for (i = 0; i < numthreads; i++) {
    ids[i] = i;
    int * p = &ids[i] ;
    pthread_create(&threads[i], NULL, function1, (void *)p);
}

但这给了我错误:

>> mpicc -o hprogram hprogram.c
warning: incompatible pointer types passing 'void (void *)' to
      parameter of type 'void * _Nullable (* _Nonnull)(void * _Nullable)'
      [-Wincompatible-pointer-types]
                        pthread_create(&threads[i], NULL, function1, (void *)...
                                                          ^~~~~~~~~~
/usr/include/pthread.h:328:31: note: passing argument to parameter here
                void * _Nullable (* _Nonnull)(void * _Nullable),
                                            ^
1 warning generated.


这是一个mpi程序,我使用pthreads创建了一个混合mpi。

1 个答案:

答案 0 :(得分:3)

pthread_create()期望指向一个以void*作为输入并返回void*作为输出的函数的指针,但是您的函数将返回void。您只需要向返回类型添加*,然后添加return语句,例如:

void* function1(void *s) {
    start = *(int *)s;
    return NULL; // <-- or whatever you want
}