简体   繁体   English

从不兼容的指针类型 [-Wincompatible-pointer-types] 获取传递“pthread_create”参数 3 的警告

[英]Getting warning of passing argument 3 of ‘pthread_create’ from incompatible pointer type [-Wincompatible-pointer-types]

I am running the below code and it is working fine but still it is giving some warning which i don't understand.我正在运行下面的代码,它工作正常,但它仍然给出了一些我不明白的警告。 Can someone please explain this to me?有人可以向我解释一下吗? Thanks谢谢

#include <stdio.h>
#include <pthread.h>

void* the_thread_func(double data_for_thread[]) {
  /* Do something here? */

        for(int i=0;i<3;i++){
    double sum = sum + data_for_thread[i];
    printf("The sum done by the_thread_func()  is = %f\n",sum);
   }

  return NULL;
}

int main() {
  printf("This is the main() function starting.\n");

  double data_for_thread[3];
  data_for_thread[0] = 5.7;
  data_for_thread[1] = 9.2;
  data_for_thread[2] = 1.6;

  /* Start thread. */
  pthread_t thread;
  printf("the main() function now calling pthread_create().\n");
  pthread_create(&thread, NULL, the_thread_func, data_for_thread);

  printf("This is the main() function after pthread_create()\n");

  /* Do something here? */

   for(int i=0;i<3;i++){
   double sum = sum + data_for_thread[i];
    printf("The sum done by main() is = %f\n",sum);
   }

  /* Wait for thread to finish. */
  printf("the main() function now calling pthread_join().\n");
  pthread_join(thread, NULL);

  return 0;
}

warning: passing argument 3 of 'pthread_create' from incompatible pointer type [-Wincompatible-pointer-types] pthread_create(&thread, NULL, the_thread_func, data_for_thread);警告:从不兼容的指针类型 [-Wincompatible-pointer-types] pthread_create(&thread, NULL, the_thread_func, data_for_thread); ^~~~~~~~~~~~~~~ In file included from thread_data.c:2:0: /usr/include/pthread.h:234:12: note: expected 'void * (*)(void *)' but argument is of type 'void * (*)(double *)' extern int pthread_create (pthread_t *__restrict __newthread, ^~~~~~~~~~~~~~~ 在thread_data.c:2:0: /usr/include/pthread.h:234:12: 注意:预期'void * (*)(void *)' but argument is of type 'void * (*)(double *)' extern int pthread_create (pthread_t *__restrict __newthread,

According to the manual pthread_create needs to be given a function with this signature:根据手册pthread_create需要给出一个具有此签名的 function:

void* (*start_routine)(void*)

But you are passing it a function that accepts double* here:但是您传递给它的 function 在这里接受double*

void* the_thread_func(double data_for_thread[]) // decays to double*

I think you need to change the signature and cast the void* inside the function like this:我认为您需要更改签名并将void*投射到 function 中,如下所示:

// accept void*
void* the_thread_func(void* vp) {
  /* Do something here? */

    double* data_for_thread = reinterpret_cast<double*>(vp); // cast here

    for(int i=0;i<3;i++){
        double sum = sum + data_for_thread[i];
        printf("The sum done by the_thread_func()  is = %f\n",sum);
    }

    return nullptr;
}

暂无
暂无

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

相关问题 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“接受”的参数 2 - warning: passing argument 2 of ‘accept’ from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型 [-Wincompatible-pointer-types]| 传递 'transform_labels' 的参数 2 | - Warning: passing argument 2 of 'transform_labels' from incompatible pointer type [-Wincompatible-pointer-types]| 警告:从不兼容的指针类型 [-Wincompatible-pointer-types] 传递 'display' 的参数 1 - warning: passing argument 1 of 'display' from incompatible pointer type [-Wincompatible-pointer-types] 警告:从不兼容的指针类型返回 [-Wincompatible-pointer-types]| - warning: return from incompatible pointer type [-Wincompatible-pointer-types]| 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“send_to_host”的参数 2 - passing argument 2 of ‘send_to_host’ from incompatible pointer type [-Wincompatible-pointer-types] 错误:- 从不兼容的指针类型 [-Wincompatible-pointer-types] 传递“get_string”的参数 1 - ERROR :- passing argument 1 of 'get_string' from incompatible pointer type [-Wincompatible-pointer-types] 来自不兼容指针类型的赋值[Wincompatible-pointer-types] - Assignment from incompatible pointer type[Wincompatible-pointer-types] 来自不兼容的指针类型[-Wincompatible-pointer-types]的赋值 - assignment from incompatible pointer type [-Wincompatible-pointer-types] RecursiveFree函数-警告:从不兼容的指针类型[-Wincompatible-pointer-types]进行初始化 - RecursiveFree Function - Warning: initialization from incompatible pointer type [-Wincompatible-pointer-types] C中的函数指针:警告:来自不兼容指针类型[-Wincompatible-pointer-types]的赋值 - Function pointers in C: warning: assignment from incompatible pointer type [-Wincompatible-pointer-types]
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM