【问题标题】:incompatible pointer types passing 'void (void *) to parameter of type 'void *不兼容的指针类型将'void(void *)传递给'void *类型的参数
【发布时间】:2019-09-24 20:48:41
【问题描述】:

我创建的 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。

【问题讨论】:

    标签: c pthreads


    【解决方案1】:

    pthread_create() 需要一个指向函数的指针,该函数将 void* 作为输入并返回 void* 作为输出,但您的函数将返回 void。你只需要在返回类型中添加一个*,并添加一个return 语句,例如:

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

    【讨论】:

      猜你喜欢
      • 2021-07-03
      • 2016-11-23
      • 1970-01-01
      • 1970-01-01
      • 2021-06-29
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多