I am confused on union vs structure in terms of memory location
Structure Program
1 2
Union Program
2 2
What is meaning of union variables occupy the same memory, and structure variable occupy separate memory location
Structure Program
Code:
#include<stdio.h>
struct s{ int x; int y;}a;
int main ()
{
a.x = 1;
a.y = 2;
printf ("%d ", a.x);
printf ("%d ", a.y);
return 0;
}
Union Program
Code:
#include<stdio.h>
union u{ int x; int y;}a;
int main ()
{
a.x = 1;
a.y = 2;
printf ("%d ", a.x);
printf ("%d ", a.y);
return 0;
}
What is meaning of union variables occupy the same memory, and structure variable occupy separate memory location
Last edited: