This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Is there a way around "incompatible pointer" warnings for void** arguments ?
- From: "Chris Hall" <chris dot hall at highwayman dot com>
- To: "'gcc-help'" <gcc-help at gcc dot gnu dot org>
- Date: Fri, 23 May 2014 13:01:25 +0100
- Subject: Is there a way around "incompatible pointer" warnings for void** arguments ?
- Authentication-results: sourceware.org; auth=none
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