0
Q:

union in c

#include <stdio.h> 
  
union test { 
    int x; 
    char y; 
}; 
  
int main() 
{ 
    union test p1; 
    p1.x = 65; 
  
    // p2 is a pointer to union p1 
    union test* p2 = &p1; 
  
    // Accessing union members using pointer 
    printf("%d %c", p2->x, p2->y); 
    return 0; 
} 
0
#include <stdio.h> 
  
// Declaration of union is same as structures 
union test { 
    int x, y; 
}; 
  
int main() 
{ 
    // A union variable t 
    union test t; 
  
    t.x = 2; // t.y also gets value 2 
    printf("After making x = 2:\n x = %d, y = %d\n\n", 
           t.x, t.y); 
  
    t.y = 10; // t.x is also updated to 10 
    printf("After making y = 10:\n x = %d, y = %d\n\n", 
           t.x, t.y); 
    return 0; 
} 
0
#include <stdio.h> 
  
union test1 { 
    int x; 
    int y; 
} Test1; 
  
union test2 { 
    int x; 
    char y; 
} Test2; 
  
union test3 { 
    int arr[10]; 
    char y; 
} Test3; 
  
int main() 
{ 
    printf("sizeof(test1) = %lu, sizeof(test2) = %lu, "
           "sizeof(test3) = %lu", 
           sizeof(Test1), 
           sizeof(Test2), sizeof(Test3)); 
    return 0; 
} 
0

New to Communities?

Join the community