casting single- to double- pointers?

C

Charlie Zender

Hi,

I have a function that takes a void pointer (vp) input prototyped as

const void * const vp

Thus vp is a constant pointer to a void which is const.
This routine passes vp to another routine that expects a

const char **vp

Thus it expects a pointer to a pointer to a char which is const.
Hence when the first routine passes vp to the second routine,
it uses an explicit cast

(const char **)vp

The compiler (gcc 4.3.x) completes this cast but warns me:

attention : passing argument 4 of ‘nc_put_var1_string’ from
incompatible pointer type

I thought C allowed casting void pointers to any other pointers.
What is the correct way to cast vp so as not to get a warning?
Is casting a singly-indirect pointer to a doubly-indirect ever
warning-free?

Thanks,
Charlie
 
B

Ben Bacarisse

Charlie Zender said:
I have a function that takes a void pointer (vp) input prototyped as

const void * const vp

Thus vp is a constant pointer to a void which is const.
This routine passes vp to another routine that expects a

const char **vp

Thus it expects a pointer to a pointer to a char which is const.
Hence when the first routine passes vp to the second routine,
it uses an explicit cast

(const char **)vp

The compiler (gcc 4.3.x) completes this cast but warns me:

attention : passing argument 4 of ‘nc_put_var1_string’ from
incompatible pointer type

This does not sound right. I can't get that message from code that is
as you describe. I'd rather see an actual example that I can pass to a
compiler.
I thought C allowed casting void pointers to any other pointers.
What is the correct way to cast vp so as not to get a warning?
Is casting a singly-indirect pointer to a doubly-indirect ever
warning-free?

It is for me, which is why I'd like to see your real code.

BTW, what you describe is not "const safe" (that is, in fact, the only
warning that gcc gives me) so it looks like at least one of types
involved is wrong. You are often forced into such a position by a
badly defined API, but if it can avoided, I would do so.
 
N

Nick Keighley

I have a function that takes a void pointer (vp) input prototyped as

const void * const vp

that is not a valid prototype. Could you just post the function's
declaration? Or better still its definition.

<snip>
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,982
Messages
2,570,190
Members
46,740
Latest member
AdolphBig6

Latest Threads

Top