A Little about the blog.....

This blog contains all the interview questions that i face during my interviews with various MNC's. I am trying to post the solutions for almost all the questions and i have covered few important topics which would be frequent topics in interviews. I hope the blog would be useful not only for the job aspirants but also to the one who tries to upgrade his/her knowledge in C and Embedded concepts......

please enter your mail address in the above mail address box to get the immediate updates of the new interview questions.....

Wednesday, November 16, 2011

difference between structures and unions

A structure is a group of variables of different data types whereas even an union is a group of variables of different data types.

what makes the union different from structure is the memory allocated for each variable in an union is the space required for the largest member in the union.
where as in the case of a structure the memory is allocated differently for different data type variables.

more ever,in the case of a union,a union can accommodate only one variable at a time, when a new variable enters, it automatically erases the old variable and new variable is stored in the place of old variable.


Explanation:
A union, is a collection of variables of different types, just like a structure. However, with unions, you can only store information in one field at any one time.

You can picture a union as like a chunk of memory that is used to store variables of different types. Once a new value is assigned to a field, the existing data is wiped over with the new data.

A union can also be viewed as a variable type that can contain many different variables (like a structure), but only actually holds one of them at a time (not like a structure). This can save memory if you have a group of data where only one of the types is used at a time. The size of a union is equal to the size of it's largest data member. In other words, the C compiler allocates just enough space for the largest member. This is because only one member can be used at a time, so the size of the largest, is the most you will need.

No comments:

Post a Comment