#include<stdio.h>
struct mystruct
{
char cc;
float abc;
};
union sample
{
int a;
float b;
char c;
double d;
struct mystruct s1;
};
int main()
{
union sample u1;
int k;
u1.s1.abc=5.5;
u1.s1.cc='a';
printf("\n%c %f\n",u1.s1.cc,u1.s1.abc);
k=sizeof(union sample);
printf("%d\n\n",k);
return 0;
}
The size of operator is returning 8
I am still able to access the structure elements, more than one at a time and still the sizeof
operator is returning the max size of primitive data types i assume. Why is this behavior? Is the size actually allocated is 8
? and the sizeof
is returning a wrong value? Or is the actual allocated size is 8
? Then how is the structure accommodated?? If we allocate an array of unions using malloc
and sizeof
will it allocate enough space in such case? Please eloborate.