How to define anonymous struct inside anonymous union inside struct in C

1 Answer

0 votes
#include <stdio.h>

struct S 
{
   union { // anonymous union
      struct { int i, j; }; // anonymous structure
      struct { long x, y; } ist; // inner structure 
   };
   int n;
} s1;
 

int main(void)
{
    s1.i = 104;  
    printf("s1.i = %d\n", s1.i);
    
    //s1.x = 7;   // Error
    s1.ist.x = 13; 
    printf("s1.ist.x = %ld\n", s1.ist.x);
        
    printf("s1.i = %d\n", s1.i);
        
    return 0;
}

/*
run:
 
s1.i = 104
s1.ist.x = 13
s1.i = 13

*/

 



answered Aug 29, 2016 by avibootz

Related questions

1 answer 164 views
1 answer 172 views
172 views asked Dec 27, 2020 by avibootz
2 answers 162 views
2 answers 313 views
1 answer 224 views
1 answer 133 views
...