C语言 C 警告:不兼容的指针类型传递

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/27285565/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-02 11:35:57  来源:igfitidea点击:

C warning: incompatible pointer types passing

cpointersparameterspthreadsincompatibletypeerror

提问by jgabb

I keep getting an error when trying to compile my code. The error is as follows :

尝试编译我的代码时,我不断收到错误消息。错误如下:

warning: incompatible pointer types passing
  'void *(threadData *)' to parameter of type 'void * (*)(void *)'
  [-Wincompatible-pointer-types]
        pthread_create(&threads[id], NULL, start,&data[id]);

I'm trying to pass a struct to the function, void * start(threadData* data), and this keeps throwing me off. Any ideas?

我试图将一个结构传递给函数,void * start(threadData* data),这一直让我失望。有任何想法吗?

回答by paxdiablo

It's complaining about the thread function (bound to the third parameter of pthread_create), you can modify that to take a void *argument and then cast it back before doing anything with it:

它在抱怨线程函数(绑定到 的第三个参数pthread_create),您可以修改它以接受一个void *参数,然后在对其进行任何操作之前将其强制转换:

void *start (void *voidData) {
    threadData *data = voidData;
    // rest of code here, using correctly typed data.

You mayalso opt to coerce the data pointer (fourth parameter) to the expected type:

可以选择将数据指针(第四个参数)强制为预期类型:

(void*)(&(data[id]))

but I don't think that's necessary since a void *is supposed to be freely convertible to and from most other pointers.

但我认为这没有必要,因为 avoid *应该可以与大多数其他指针自由转换。



You can see the problem in this small yet complete program:

您可以在这个小而完整的程序中看到问题:

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

struct sData { char text[100]; };

void *start (struct sData *data) {
        printf ("[%s]\n", data->text);
}

int main (void) {
        struct sData sd;
        pthread_t tid;
        int rc;

        strcpy (sd.text, "paxdiablo");
        rc = pthread_create (&tid, NULL, start, &sd);
        pthread_join (tid, NULL);

        return 0;
}

When compiling that, you see:

编译时,您会看到:

prog.c: In function 'main':
prog.c:20:2: warning: passing argument 3 of 'pthread_create' from
             incompatible pointer type [enabled by default]
             In file included from prog.c:3:0:
                 /usr/include/pthread.h:225:12: note: expected
                     'void * (*)(void *)' but argument is of type
                     'void * (*)(struct sData *)'

Keep in mind that it's just a warning,not an error but, if you want your code to compile cleanly, it's worth getting rid of. Making the changes mentioned at the top of this answer (bar the data parameter casting) gives you the following thread function:

请记住,这只是一个警告,而不是一个错误,但是,如果您希望代码能够干净利落地编译,则值得摆脱。进行此答案顶部提到的更改(禁止数据参数转换)为您提供以下线程函数:

void *start (void *voidData) {
        struct sData *data = voidData;
        printf ("[%s]\n", data->text);
}

This compiles without warnings, and runs just fine.

这在没有警告的情况下编译,并且运行得很好。