I have a question regarding void* and void** and I know this is sort of an old question and has been asked (somewhat) before in stackoverflow. So the question is the following:
When I compile this code with gcc 4.4.3 under ubuntu 10.10, I get the following warning:
zz.c: In function ‘main’:
zz.c:21: warning: passing argument 1 of ‘bar’ from incompatible pointer type
zz.c:9: note: expected ‘void **’ but argument is of type ‘float **’
why is it that its ok to pass variable x as an argument of foo() but its not ok to pass variable y as an argument of bar(). I can fix this by explicitly casting both variables into void* and void** as expected.
void foo (void* a){
}
void bar(void **a){
*a = (float *) malloc(100*sizeof(float));
}
int main (){
float *x = (float*) malloc(100*sizeof(float));
foo(x);
free(x);
float *y;
bar(&y);
free(y);
return 0;
}
void *ameans thatapoints to an object of unknown type. However,aitself is not an unknown type becauseais known to have typevoid*. Only the object thatapoints to has an unknown type.void **ameans thatapoints to an object of typevoid*. The object that*apoints to has an unknown type, but the object*aitself is a pointer with typevoid*.&yis a pointer to an object of typefloat*.&yis not a pointer to an object of typevoid*.