Sujet : Re: do { quit; } else { }
De : david.brown (at) *nospam* hesbynett.no (David Brown)
Groupes : comp.lang.cDate : 09. Apr 2025, 13:06:45
Autres entêtes
Organisation : A noiseless patient Spider
Message-ID : <vt5nsl$inuo$2@dont-email.me>
References : 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
User-Agent : Mozilla/5.0 (X11; Linux x86_64; rv:102.0) Gecko/20100101 Thunderbird/102.11.0
On 09/04/2025 12:04, Michael S wrote:
On Wed, 9 Apr 2025 11:42:36 +0200
David Brown <david.brown@hesbynett.no> wrote:
>
In C, if you declare two structs in the same translation unit with
the same field types and the same field names, they are still
different types.
That is clear for structs with tags. Less clear for tagless structs
that have exactly the same fields.
(Note - I am referencing C11 here for personal convenience, but the section numbering and contents is, AFAIR, the same from C99 up to C17. C23 changed the numbering a bit.)
6.7.2.1 "Structure and union specifiers" paragraph 8:
"""
The presence of a struct-declaration-list in a struct-or-union-specifier declares a new type, within a translation unit.
"""
So two tagless structs with the same fields declared, compatibility is the same as always - within a translation unit, they declare distinct incompatible new types, while across translation units they are compatible.
6.2.7 "Compatible type and composite type" paragraph 1:
"""
Two types have compatible type if their types are the same. Additional rules for determining whether two types are compatible are described in 6.7.2 for type specifiers, in 6.7.3 for type qualifiers, and in 6.7.6 for declarators. 55) Moreover, two structure, union, or enumerated types declared in separate translation units are compatible if their tags and members satisfy the following requirements: If one is declared with a tag, the other shall be declared with the same tag. If both are completed anywhere within their respective translation units, then the following additional requirements apply: there shall be a one-to-one correspondence between their members such that each pair of corresponding members are declared with compatible types;
"""