bogus "assignment from incompatible pointer type" ?

Nathan Sidwell
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?

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[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 <>)
told me.

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;
In C++ you can add inner qualifiers, provided all the outer ones are const,
which is also safe.



Dr Nathan Sidwell   ::   ::   CodeSourcery LLC
         'But that's a lie.' - 'Yes it is. What's your point?' : :

More information about the Gcc-bugs mailing list