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 ?

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;
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.