GeekInterview.com
Series: Subject: Topic:
Question: 150 of 238

what is the difference between structure and union?

Asked by: Interview Candidate | Asked on: Feb 8th, 2006
Showing Answers 1 - 6 of 6 Answers
Yashwant S pinge

Answered On : Mar 6th, 2006

The union is a structure.The main difference between structure and union isThe size of the union is equal to the size of the largest member of the union where as size of the structrue is the sum of the size of al members of the structure.And one more thing is that we can use one member of the union at a time.

  
Login to rate this answer.
venkateswararao

Answered On : Oct 17th, 2006

hello sir

           please give me clear explanation with example please send immidieate answer for this question

                                  thank u

  
Login to rate this answer.

While structure enables us treat a number of different variables stored at different in memory , a union enables us to treat the same space in memory as a number of different variables. That is a Union offers a way for a section of memory to be treated as a variable of one type on one occasion and as a different variable of a different type on another occasion.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members.

Example:

union exforsys_t {
  char c;
  int i;
  float f;
  } exforsys;
 

Defines three elements:

exforsys.c
exforsys.i
exforsys.f

Each one with a different data type. Since all of them are referring to the same location in memory, the modification of one of the elements will affect the value of all of them. We cannot store different values in them independent from each other.

  
Login to rate this answer.
Rajiv Podar

Answered On : Nov 20th, 2006

The difference between structure and union in c are: 1. union allocates the memory equal to the maximum memory required by the member of the union but structure allocates the memory equal to the total memory required by the members. 2. In union, one block is used by all the member of the union but in case of structure, each member have their own memory space

  
Login to rate this answer.
vikasrulz

Answered On : Aug 10th, 2008

View all answers by vikasrulz

Agreed UNION uses just one location (max among all the menbers of the UNION), but then say my UNION has int and float, initially I read a int value, it gets stored in a particular location. Now when I read the float value, I lose my int value because i will be overwriting the int value.

so my question is where does the previous value get stored?

  
Login to rate this answer.
sendhil

Answered On : Aug 14th, 2011

structure has different memory location but union has the same memory location while using the structure , struct is the keyword and here union is the keyword

  
Login to rate this answer.

Give your answer:

If you think the above answer is not correct, Please select a reason and add your answer below.

Related Open Questions

Ads

Connect

twitter fb Linkedin GPlus RSS

Ads

Interview Question

 Ask Interview Question?

 

Latest Questions

Interview & Career Tips

Get invaluable Interview and Career Tips delivered directly to your inbox. Get your news alert set up today, Once you confirm your Email subscription, you will be able to download Job Inteview Questions Ebook . Please contact me if you there is any issue with the download.