bogus "assignment from incompatible pointer type" ?

Nathan Sidwell nathan@codesourcery.com
Mon May 15 02:40:00 GMT 2000


Zack Weinberg wrote:
> 
> gcc complains about "assignment from incompatible pointer type" in
> this code:
> 
> const struct hashnode **foo;
> void x(struct hashnode **y)
> {
>   foo = y;
> }
> 
> but does not object to the assignment if there is only one layer of
> pointers, or 'foo' is declared as 'struct hashnode *const *'.  Why is
> this?

http://www.eskimo.com/~scs/C-faq/q11.10.html

which could be paraphrased as `because it says so'. It's cos you can
then silently modify a const without a cast. Here's what
http://x26.deja.com/[ST_rn=ps]/getdoc.xp?AN=570124515&CONTEXT=958383045.2072444954&hitnum=2
(Forum: comp.lang.c
 Thread: Incompatible pointer type question 
 Date:  01/08/2000
 Author: Lawrence Kirby <fred@genesis.demon.co.uk>)
told me.

<quote>
So what would be the problem if C allowed a T ** pointer to be
 converted implicitly to const T**? Consider the sequence:
  
   const int ci = 42;
   const int *pci = &ci;
   int *pi;
   int **ppi = π
   const int **ppci = ppi;  /* The critical step that C forbids, ppci now points to pi */
   *ppci = pci;              /* pi now points to ci */
   *pi = 43;
</quote>
In C++ you can add inner qualifiers, provided all the outer ones are const,
which is also safe.

OTH

nathan

-- 
Dr Nathan Sidwell   ::   http://www.codesourcery.com   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?'
nathan@codesourcery.com : http://www.cs.bris.ac.uk/~nathan/ : nathan@acm.org


More information about the Gcc-bugs mailing list