R
ranjeet.gupta
Dear ALL
As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and compiler), and it is,
what I conclude from the below code
union data_type {
unsigned int a;
char b[2];
};
<--------------Unsigned int a------------------>
-----------------------------------------------
| | | | |
------------------------------------------------
<--b[0]--><---b[1]--->
int main ()
{
union data_type d;
d.a = 512 * 512;
printf("%d\n", d.a);
printf("%d\n %d\n %d\n %d\n ", d.b[0],d.b[1],d.b[2],d.b[3]);
getchar();
return 0;
}
when i write the above code the it means that
<--------------Unsigned int a-------->
-------------------------------------
|00000000|00000000|00000010|0000000|
-------------------------------------
<--b[0]-><--b[1]->
Now when I specificaaly Allcoate the char for two bytes array,
then why on fetching its is fetching the value of b[2] and b[3]
Is the internal padding for the char is also going on in union.
thats is making to the 4 bytes char, Does Union also support the
padding ? (Padding is implementation use)
Please let me know what is happening and what I understood wrong ?
Regards
Ranjeet
As we know that when we declare the union then we have the size of the
union which is the size of the highest data type as in the below case
the
size should be 4 (For my case and compiler), and it is,
what I conclude from the below code
union data_type {
unsigned int a;
char b[2];
};
<--------------Unsigned int a------------------>
-----------------------------------------------
| | | | |
------------------------------------------------
<--b[0]--><---b[1]--->
int main ()
{
union data_type d;
d.a = 512 * 512;
printf("%d\n", d.a);
printf("%d\n %d\n %d\n %d\n ", d.b[0],d.b[1],d.b[2],d.b[3]);
getchar();
return 0;
}
when i write the above code the it means that
<--------------Unsigned int a-------->
-------------------------------------
|00000000|00000000|00000010|0000000|
-------------------------------------
<--b[0]-><--b[1]->
Now when I specificaaly Allcoate the char for two bytes array,
then why on fetching its is fetching the value of b[2] and b[3]
Is the internal padding for the char is also going on in union.
thats is making to the 4 bytes char, Does Union also support the
padding ? (Padding is implementation use)
Please let me know what is happening and what I understood wrong ?
Regards
Ranjeet