Nathan
Nathan

Reputation: 483

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?

Upvotes: 5

Views: 38652

Answers (3)

Mani
Mani

Reputation: 17595

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);.

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

Upvotes: 0

caf
caf

Reputation: 239341

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.

Upvotes: 7

anshul garg
anshul garg

Reputation: 503

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

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

Use it like this.

Upvotes: -1

Related Questions