This is the mail archive of the gcc-help@gcc.gnu.org mailing list for the GCC project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]
Other format: [Raw text]

Is there a way around "incompatible pointer" warnings for void** arguments ?


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


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]