What is the difference between structure and union?

Showing Answers 1 - 15 of 15 Answers

Yashwant S pinge

  • 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.

  Was this answer useful?  Yes

venkateswararao

  • Oct 17th, 2006
 

hello sir

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

                                  thank u

  Was this answer useful?  Yes

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.

  Was this answer useful?  Yes

Rajiv Podar

  • 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

  Was this answer useful?  Yes

vikasrulz

  • Aug 10th, 2008
 

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?

  Was this answer useful?  Yes

sendhil

  • 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

  Was this answer useful?  Yes

Nidhi.saradhara

  • May 5th, 2014
 

The difference between Union and structure is the structure is occupied a memory to all member in the structure but union is occupied to memory in largest member in union

  Was this answer useful?  Yes

Give your answer:

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

 

Related Answered Questions

 

Related Open Questions