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