C语言 C 中不兼容的指针类型

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/16160799/
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 06:08:20  来源:igfitidea点击:

incompatible pointer type in C

c

提问by Nathan

so I'm trying to pass a type double *to a function that accepts void **as one of the parameters. This is the warning that I am getting.

所以我试图将一个类型传递double *给一个接受void **作为参数之一的函数。这是我收到的警告。

incompatible pointer type passing 'double **' to parameter of type 'void **'

Here is a snippet of my code.

这是我的代码片段。

int main( void )
{
    //  Local Declaration
    double *target;

   //   Statement
   success = dequeue(queueIn, &target);
}

Here's the prototype declaration of the function.

这是函数的原型声明。

int    dequeue     ( QUEUE *queue, void **dataOutPtr );

I thought that if I passed target as a two level pointer that it would work, but I guess I'm wrong. Can someone please explain to me how come i'm getting this warning?

我认为如果我将目标作为两级指针传递,它会起作用,但我想我错了。有人可以向我解释我怎么会收到这个警告吗?

回答by caf

Even though all other pointer types can be converted to and from void *without loss of information, the same is not true of void **and other pointer-to-pointer types; if you dereference a void **pointer, it needs to be pointing at a genuine void *object1.

尽管所有其他指针类型都可以在void *不丢失信息的情况下相互转换,但void **其他指针到指针类型并非如此;如果您取消引用一个void **指针,它需要指向一个真正的void *对象1

In this case, presuming that dequeue()is returning a single pointer value by storing it through the provided pointer, to be formally correct you would need to do:

在这种情况下,假设dequeue()通过提供的指针存储它来返回单个指针值,要正式正确,您需要执行以下操作:

int main( void )
{
    void *p;
    double *target;

    success = dequeue(queueIn, &p);
    target = p;

When you write it like this, the conversion from void *to double *is explicit, which allows the compiler to do any magic that's necessary (even though in the overwhelmingly common case, there's no magic at all).

当您像这样编写它时,从void *to的转换double *是显式的,这允许编译器执行任何必要的魔术(即使在绝大多数情况下,根本没有魔术)。



1. ...or a char *, unsigned char *or signed char *object, because there's a special rule for those.1. ......或者char *unsigned char *signed char *对象,因为有一个特殊的规则相同。

回答by Mani

In your prototype declaration , you said second argument as void**,so you have to type cast double**to void**. Instead of this line success = dequeue(queueIn, &target);.

在你的原型声明中,你说第二个参数 as void**,所以你必须输入 cast double**to void**。而不是这条线success = dequeue(queueIn, &target);

Call like this success = dequeue(queueIn,(void**) &target);

像这样打电话 success = dequeue(queueIn,(void**) &target);

回答by anshul garg

int main( void )
{
    //  Local Declaration
    double *target;

   //   Statement
   success = dequeue(queueIn, (void**)&target);
}

Use it like this.

像这样使用它。