Is there a way around "incompatible pointer" warnings for void** arguments ?
Chris Hall
chris.hall@highwayman.com
Fri May 23 12:02:00 GMT 2014
Where I have something along the lines of:
typedef struct foo* foo
void
create(void** p_n, size_t s)
{
*p_n = malloc(s) ;
} ;
.....
{
foo bar ;
.....
create(&bar, 86) ;
.....
} ;
I get the helpful warning "passing argument 1 of 'create' from incompatible pointer type". (This is, obviously, an artificial example. The problem arises with any function which takes a void** argument.)
Don't get me wrong, I'm a big fan of this warning, in general. I suspect the reason is something deep in C... but it doesn't make full sense to me.
I can of course write:
create((void**)&bar, 86) ;
but if I write:
create((void**)bar, 86) ;
The compiler lets that past and leaves it to be found during testing :-(
And similarly:
create((void*)bar, 86) ;
And (bizarrely, IMHO):
create(&((void*)bar), 86) ;
is an error: "lvalue required as unary '&' operand" ?!?
Pointers to pointers and, worse yet, chains of pointers to pointers require care (and my head hurts). So I appreciate all the help the compiler can give me to spot problems early. Having to cast to (void**) feels like taking the seat belt off and pressing the pedal to the metal :-(
Chris
More information about the Gcc-help
mailing list