Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
493 views
in Technique[技术] by (71.8m points)

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 **'

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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.

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. ...or a char *, unsigned char * or signed char * object, because there's a special rule for those.

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...