Union of structs with duplicate var names

J

James Waldby

This post includes a program that sets values of b.d, b.e, b.f, b.g
and prints values of b.d, b.e, b.f, b.g, b.h, b.i, b.j, b.k after
it declares:
struct {
union {
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

Some readers may wish to imagine what compiler messages appear
from "gcc union-test2.c -Wall -W -o union-test2" or the same
with -pedantic or -ansi added, and/or what the program output
looks like, before reading much further. And what the output
looks like when the order of lines S2 and S3 is reversed.

Can anyone point to specific paragraphs of C standards regarding
duplicated variable names? I think C standards require that names
of member variables be distinct within a structure or union and
thought that should rule out declarations like the above. (If
the inner structs weren't anonymous, there would be no issues.)

Here is the program:

#include <stdio.h>
int main() {
struct {
union {
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

b.d = 34; b.e = 35; b.f = 36; b.g = 37;

printf (" d %d", b.d);
printf (" e %d", b.e);
printf (" f %d", b.f);
printf (" g %d\n", b.g);
printf (" h %d", b.h);
printf (" i %d", b.i);
printf (" j %d", b.j);
printf (" k %d\n", b.k);
return 0;
}

Compiler results are given below, after dots.

..

..

..

With gcc 4.1.2 and
gcc union-test.c -Wall -W -o union-test
the program compiles ok and has no warnings.

With -pedantic switch added it gets six warnings, including
"union has no named members", "struct has no named members",
and "ISO C doesn’t support unnamed structs/unions"

With -ansi switch added it gets ten errors and four warnings
that include messages like "‘struct <anonymous>’ has no member
named ‘k' " and "declaration does not declare anything".

Program output is shown below, after dots.

..

..

..

d 34 e 35 f 36 g 37
h 36 i 34 j 37 k 35

Following is program output when S2 and S3 are reversed:

d 34 e 35 f 36 g 37
h 34 i 35 j 36 k 37
 
E

Ersek, Laszlo

struct {
union {
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

C99 6.7.2.1 "Structure and union specifiers", p7

----v----
The presence of a struct-declaration-list in a struct-or-union-specifier
declares a new type, within a translation unit. The
struct-declaration-list is a sequence of declarations for the members of
the structure or union. If the struct-declaration-list contains no named
members, the behavior is undefined. The type is incomplete until after the
} that terminates the list.
----^----

Cheers,
lacos
 
B

Ben Bacarisse

James Waldby said:
This post includes a program that sets values of b.d, b.e, b.f, b.g
and prints values of b.d, b.e, b.f, b.g, b.h, b.i, b.j, b.k after
it declares:
struct {
union {
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

Some readers may wish to imagine what compiler messages appear
from "gcc union-test2.c -Wall -W -o union-test2" or the same
with -pedantic or -ansi added, and/or what the program output
looks like, before reading much further. And what the output
looks like when the order of lines S2 and S3 is reversed.

Can anyone point to specific paragraphs of C standards regarding
duplicated variable names? I think C standards require that names
of member variables be distinct within a structure or union and
thought that should rule out declarations like the above. (If
the inner structs weren't anonymous, there would be no issues.)

You have to step back from what appear to you to be duplicated names in
the union. Standard C not permit anonymous struct members so the above
is a syntax error. It is more helpful for the compiler (when in
conforming mode) to tell you this than to complain about a consequences
of this syntax error.

Your complaint against gcc when invoked in non-conforming mode seems to
be a valid one. A compiler that permits this extension should probably
warn about duplicate member names that don't happen to coincide, but
that is QOI issue about gcc. The C standard says nothing about it
except that the program contains a syntax error.

<snip>
 
B

Ben Bacarisse

Ersek said:
C99 6.7.2.1 "Structure and union specifiers", p7

----v----
The presence of a struct-declaration-list in a
struct-or-union-specifier declares a new type, within a translation
unit. The struct-declaration-list is a sequence of declarations for
the members of the structure or union. If the struct-declaration-list
contains no named members, the behavior is undefined. The type is
incomplete until after the } that terminates the list.
----^----

That's not really the point. If the code had been:

struct {
int x;
union {
int y;
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

neither the struct or the union would have no named members, but the
code would still not be standard C.
 
E

Ersek, Laszlo

That's not really the point. If the code had been:

struct {
int x;
union {
int y;
struct { int f, d, g, e; }; /* S2 */
struct { int d, e, f, g; }; /* S3 */
struct { int h, i, j, k; };
};
} b;

neither the struct or the union would have no named members, but the
code would still not be standard C.

Yes, sorry. 6.7.2.1 p1

----v----
struct-declaration:
specifier-qualifier-list struct-declarator-list ;

struct-declarator-list:
struct-declarator
struct-declarator-list , struct-declarator

struct-declarator:
declarator
declarator_opt : constant-expression
----^----

was violated, "struct-declarator-list" can't be empty. p7 probably says
"If the struct-declaration-list contains no named members, the behavior is
undefined", because the p1, p3 and p11 allow/require unnamed bit-fields.

struct s
{
unsigned
u0:2,
:1,
u1:2,
:0,
u2:2;
};

Thanks,
lacos
 

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,954
Messages
2,570,116
Members
46,704
Latest member
BernadineF

Latest Threads

Top