用C兼容的指针类型 [英] incompatible pointer type in C
问题描述
所以我想传递一个类型双*
来接受无效**
作为一个功能的参数。这是我收到的警告。
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.
incompatible pointer type passing 'double **' to parameter of type 'void **'
下面是我的code片段。
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?
推荐答案
即使所有其他指针类型可以转换为从无效*
不会丢失信息,在同样不无效**
和其他指针到指针类型的真实;如果取消引用无效**
指针,它需要在一个真正的无效*
对象 1是指向。
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.
在这种情况下,presuming的出列()
通过存储它通过提供的指针,被正式纠正你需要返回一个指针值这样做:
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:
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).
1。 ......或
的char *
,无符号字符*
或符号字符*
对象,因为有那些特殊的规则。
1. ...or a
char *
, unsigned char *
or signed char *
object, because there's a special rule for those.
这篇关于用C兼容的指针类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!