Saturday, September 13, 2008

Difference between Structure and Union.

As most of the people know the answer.Yes ur right , the size of the structure is total size of the variables where as for Union ,size is max size of the variables.

struct size
{
int i;
float f;
char c;
};

and size of this structure is 9 bytes (4(int)+4(float)+1(char) ) depending on the machine.
You may get 12bytes on some machines because of structure Padding ,i will write abt this in the next post.
union size
{
int i;
float f;
char c;
};

and the size of this union is max size of the variable i.e 4 bytes(int or flaot 4 byets).

No comments:

Popular Posts