1
union u
{
    union u
  {
      int i;
      int j;
  }a[10];
   int b[10];
}u;

main()
{
printf("n%d", sizeof(u));
printf(" %d", sizeof(u.a));
printf("%d", sizeof(u.a[4].i));
}

Hello People. I just came across this piece of C code. I have a couple of question
The third printf statement gives me the following error

pollux {~} > gcc test.c -o test
test.c: In function `main':
test.c:21: error: union has no member named `i'
pollux {~} >

I cannot understand the reason for this. Can some one shed some light on this ?

Edited by abhimanipal: n/a

2
Contributors
1
Reply
2
Views
7 Years
Discussion Span
Last Post by __avd
0

C has single namespace for all structs, unions and enums. So you can write code using anonymous union,

union u
{
   union
  {
      int i;
      int j;
  }a[10];
   int b[10];
}u;

or choose unique tagname,

union u
{
   union v
  {
      int i;
      int j;
  }a[10];
   int b[10];
}u;
This topic has been dead for over six months. Start a new discussion instead.
Have something to contribute to this discussion? Please be thoughtful, detailed and courteous, and be sure to adhere to our posting rules.