Compatible types and structures

  • Thread starter Oliver Charlesworth
  • Start date
O

Oliver Charlesworth

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.
 
A

Andrey Vul

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!









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.
 
E

Eric Sosman

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!

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.
 

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,769
Messages
2,569,581
Members
45,057
Latest member
KetoBeezACVGummies

Latest Threads

Top