This is the mail archive of the
gcc-help@gcc.gnu.org
mailing list for the GCC project.
Re: Why does 'const' make structure pointers incompatible?
- From: Andrew Haley <aph at redhat dot com>
- To: David Daney <ddaney at caviumnetworks dot com>
- Cc: gcc-help at gcc dot gnu dot org, Ralf Baechle <ralf at linux-mips dot org>
- Date: Tue, 12 May 2009 18:19:41 +0100
- Subject: Re: Why does 'const' make structure pointers incompatible?
- References: <4A09AA0B.1050607@caviumnetworks.com>
David Daney wrote:
> Consider this code (simplified from Linux kernel):
>
> ----------- foo.c -----------
> struct B {
> int f1;
> int f2;
> int f3;
> };
>
> struct A {
> int (* foo)(struct B *);
> };
>
> int bar(const struct B *a)
> {
> return a->f1 + a->f2 + a->f3;
> }
>
> struct A baz = {bar};
> -----------8<---------------
>
> $ gcc --version
> gcc (GCC) 4.3.0 20080428 (Red Hat 4.3.0-8)
> [...]
>
> $ gcc -Wall -c foo.c
> foo.c:16: warning: initialization from incompatible pointer type
>
> Why do I get the warning?
>
> In most cases a pointer to an object can be passed to functions taking a
> pointer to a const object with no warning being issued.
That's not what's happening here. const struct B* and struct B* are
different types (6.2.5 Para 25) and the type of a function that
returns Type X is "function returning X" (6.2.5 Para 23). Therefore
the pointer types are not compatible, and you get the warning.
Qualifiers really do form different types, even though they are in the
same type category.
Andrew.