Velocity Reviews

Velocity Reviews (http://www.velocityreviews.com/forums/index.php)
-   C Programming (http://www.velocityreviews.com/forums/f42-c-programming.html)
-   -   Union of structs with duplicate var names (http://www.velocityreviews.com/forums/t722620-union-of-structs-with-duplicate-var-names.html)

James Waldby 05-09-2010 11:17 PM

Union of structs with duplicate var names
 
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

--
jiw

Ersek, Laszlo 05-09-2010 11:48 PM

Re: Union of structs with duplicate var names
 
On Sun, 9 May 2010, James Waldby wrote:

> 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

Ben Bacarisse 05-10-2010 12:06 AM

Re: Union of structs with duplicate var names
 
James Waldby <no@no.no> writes:

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

Ben Bacarisse 05-10-2010 12:23 AM

Re: Union of structs with duplicate var names
 
"Ersek, Laszlo" <lacos@caesar.elte.hu> writes:

> On Sun, 9 May 2010, James Waldby wrote:
>
>> 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.
> ----^----


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.

--
Ben.

Ersek, Laszlo 05-10-2010 01:14 AM

Re: Union of structs with duplicate var names
 
On Mon, 10 May 2010, Ben Bacarisse wrote:

> 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


All times are GMT. The time now is 04:38 AM.

Powered by vBulletin®. Copyright ©2000 - 2014, vBulletin Solutions, Inc.
SEO by vBSEO ©2010, Crawlability, Inc.