Compatible types and structures

Discussion in 'C Programming' started by Oliver Charlesworth, Jun 2, 2010.

  1. I originally posted this on Stack Overflow
    (http://stackoverflow.com/questions/2904668/compatible-types-and-structures-in-c),
    but I'm not sure there was a definitive answer, so trying my luck here!

    > I have the following code:
    >
    > int main(void)
    > {
    > struct { int x; } a, b;
    > struct { int x; } c;
    > struct { int x; } *p;
    >
    > b = a; /* OK */
    > c = a; /* Doesn't work */
    > p = &a; /* Doesn't work */
    >
    > return 0;
    > }
    > which fails to compile under GCC (3.4.6), with the following error:
    >
    > test.c:8: error: incompatible types in assignment
    > test.c:9: warning: assignment from incompatible pointer type
    > Now, from what I understand (admittedly from the C99 standard), is that a and c should be compatible types, as they fulfill all the criteria in section 6.2.7, paragraph 1. I've tried compiling with std=c99, to no avail.
    >
    > Presumably my interpretation of the standard is wrong?


    Whilst some agreed that this "should" be legal, others suggested it
    wasn't because the structures were anonymous. However, I can't see such
    a preclusion in the spec.

    Any help appreciated.


    --
    Oliver
     
    Oliver Charlesworth, Jun 2, 2010
    #1
    1. Advertising

  2. Oliver Charlesworth

    Andrey Vul Guest

    On Jun 2, 3:00 am, Oliver Charlesworth <> wrote:
    > I originally posted this on Stack Overflow
    > (http://stackoverflow.com/questions/2904668/compatible-types-and-struc...),
    > but I'm not sure there was a definitive answer, so trying my luck here!
    >
    >
    >
    > > I have the following code:

    >
    > > int main(void)
    > > {
    > >     struct { int x; } a, b;
    > >     struct { int x; } c;
    > >     struct { int x; } *p;

    >
    > >     b = a;   /* OK */
    > >     c = a;   /* Doesn't work */
    > >     p = &a;  /* Doesn't work */

    >
    > >     return 0;
    > > }
    > > which fails to compile under GCC (3.4.6), with the following error:

    >
    > > test.c:8: error: incompatible types in assignment
    > > test.c:9: warning: assignment from incompatible pointer type
    > > Now, from what I understand (admittedly from the C99 standard), is that a and c should be compatible types, as they fulfill all the criteria in section 6.2.7, paragraph 1. I've tried compiling with std=c99, to no avail.

    >
    > > Presumably my interpretation of the standard is wrong?

    >
    > Whilst some agreed that this "should" be legal, others suggested it
    > wasn't because the structures were anonymous.  However, I can't see such
    > a preclusion in the spec.


    Unless you typedef the struct definition or tag it, the compiler can't
    be sure that a and c are the same type (etc. for p).
    Assigning values by force requires use of *(void **), and looks about
    as ugly as bypassing const-correctness.
     
    Andrey Vul, Jun 2, 2010
    #2
    1. Advertising

  3. Oliver Charlesworth

    Eric Sosman Guest

    On 6/2/2010 3:00 AM, Oliver Charlesworth wrote:
    > I originally posted this on Stack Overflow
    > (http://stackoverflow.com/questions/2904668/compatible-types-and-structures-in-c),
    > but I'm not sure there was a definitive answer, so trying my luck here!
    >
    >> I have the following code:
    >>
    >> int main(void)
    >> {
    >> struct { int x; } a, b;
    >> struct { int x; } c;
    >> struct { int x; } *p;
    >>
    >> b = a; /* OK */
    >> c = a; /* Doesn't work */
    >> p = &a; /* Doesn't work */
    >>
    >> return 0;
    >> }
    >> which fails to compile under GCC (3.4.6), with the following error:
    >>
    >> test.c:8: error: incompatible types in assignment
    >> test.c:9: warning: assignment from incompatible pointer type
    >> Now, from what I understand (admittedly from the C99 standard), is
    >> that a and c should be compatible types, as they fulfill all the
    >> criteria in section 6.2.7, paragraph 1. I've tried compiling with
    >> std=c99, to no avail.
    >>
    >> Presumably my interpretation of the standard is wrong?


    a and b are compatible because they are of the same type:
    an anonymous struct containing one int element named x.

    c is of another type, and *p is of yet another. Both of
    these two new types "look like" the type of a,b -- an anonymous
    struct containing one int element named x -- but they are new,
    distinct types nonetheless.

    You've overlooked one phrase of 6.2.7p1: "Moreover, two
    structure [...] type *declared in separate translation units*
    are compatible if [...]" That is, c would be compatible with
    a,b and *p with both sets if the three declarations were in three
    different translation units. Since they're all in one unit, they
    are distinct (and incompatible) types.

    This may seem odd, but in a way the real oddity is that two
    completely independent declarations compiled separately can somehow
    name compatible types. But C has no means to "pre-compile" a type
    declaration and pass it around in compiled form; the only way you
    can get different declarations to produce "the same thing" is to
    rely on a sort of coincidence: If they "look the same," they are
    "the same." But this special-case sameness is only needed between
    separately-compiled sources; within any one source, independent
    type declarations declare independent types.

    This seems a good time to mention the literary scholar who
    spent his entire career proving that the Iliad and the Odyssey
    were not written by Homer, but by another blind Greek poet of
    the same name.

    --
    Eric Sosman
    lid
     
    Eric Sosman, Jun 2, 2010
    #3
    1. Advertising

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

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. tweak
    Replies:
    14
    Views:
    2,791
    Eric Sosman
    Jun 11, 2004
  2. Alfonso Morra
    Replies:
    11
    Views:
    722
    Emmanuel Delahaye
    Sep 24, 2005
  3. max
    Replies:
    5
    Views:
    615
    Malcolm
    Jan 6, 2006
  4. nicolas.sitbon

    union field access and compatible types

    nicolas.sitbon, Jan 8, 2010, in forum: C Programming
    Replies:
    6
    Views:
    516
    Tim Rentsch
    Jan 13, 2010
  5. pantagruel
    Replies:
    0
    Views:
    249
    pantagruel
    Feb 17, 2006
Loading...

Share This Page