Home > Mobile >  Why won't GCC give errors passing a void * to a void** parameter?
Why won't GCC give errors passing a void * to a void** parameter?

Time:03-20

Shouldn't gcc give a type warning because a void* is being passed to a void** function parameter?

I tried clang, gcc4.8 and gcc9. None of them seem to care:

gcc -Wall -c t.c

void f(void *a)
{
}

void g(void **b)
{
    f(b);
}

Interestingly, the next example below does trigger this warning:

t.c: In function ‘g’:
t.c:7:2: warning: passing argument 1 of ‘f’ from incompatible pointer type [enabled by default]
  f(&b);
  ^
t.c:1:6: note: expected ‘void **’ but argument is of type ‘char *’
 void f(void **a)
void f(void **a)
{
}

void g(char b)
{
    f(&b);
}

and this next one gives:

t.c: In function ‘g’:
t.c:7:2: warning: passing argument 1 of ‘f’ from incompatible pointer type [enabled by default]
  f(&b);
  ^
t.c:1:6: note: expected ‘void **’ but argument is of type ‘char **’
 void f(void **a)
void f(void **a)
{
}

void g(char *b)
{
    f(&b);
}

but this does not:

void f(void *a)
{
}

void g(char b)
{
    f(&b);
}

so sometimes it cares about the pointer type being passed to the void** and sometimes it does not. Thus, void* and void** are not always treated the same. Also char** won't cast to void**.

CodePudding user response:

The void * type gets special treatment. It basically means a pointer to some unknown type. Any object pointer may be converted to or from a void * without a cast.

  • Related