Searching...
Friday, 20 January 2017

Difference between Structure and Union

Difference between Structure and Union




Structure and union both are user defined data types which contains variables of different data types. Both of them have same syntax for definition, declaration of variables and for accessing members. Still there are many difference between structure and union.

The main difference is the way they store things in memory.

A struct will allocate space in memory for each of it's members.

A union will use the same space in memory for each of it's members.

structs are functionally identical to classes only with the default access level for its members are public.

unions are similar to classes only they can hold a 


value for only one data member at a time. other 

differences are:


~ like structs, the default access level is public.

~ they can not have virtual members.


~ they can not inherit or be inherited from.



~ members cannot be objects that define 


constructors, destructors, or overload the assignment 

operator.


Structure
Union
In structure each member get separate space in memory. Take below example.

struct student
{
      int rollno;
      char gender;
      float marks;
}s1;

The total memory required to store a structure variable is equal to the sum of size of all the members. In above case 7 bytes (2+1+4) will be required to store structure variable s1.

In union, the total memory space allocated is equal to the member with largest size. All other members share the same memory space. This is the biggest difference between structure and union.

union student
{
      int rollno;
      char gender;
      float marks;
}s1;

In above example variable marks is of float type and have largest size (4 bytes). So the total memory required to store union variable s1 is 4 bytes.

We can access any member in any sequence.

s1.rollno = 20;
s1.marks = 90.0;
cout<<s1.rollno;

The above code will work fine but will show erroneous output in the case of union.
We can access only that variable whose value is recently stored.

s1.rollno = 20;
s1.marks = 90.0;
cout<<s1.rollno;

The above code will show erroneous output. The value of rollno is lost as most recently we have stored value in marks. This is because all the members share same memory space.

All the members can be initialized while declaring the variable of structure.
Only first member can be initialized while declaring the variable of union. In above example we can initialize only variable rollno at the time of declaration of variable.
Next
This is the most recent post.
Previous
Sum of two numbers using c language

1 comments:

  1. There are many lots of popular college, physics, chemistry, math for all subjects.

    http://testbankbyte.com/

    ReplyDelete
:) :)) ;(( :-) =)) ;( ;-( :d :-d @-) :p :o :>) (o) [-( :-? (p) :-s (m) 8-) :-t :-b b-( :-# =p~ $-) (b) (f) x-) (k) (h) (c) cheer
Click to see the code!
To insert emoticon you must added at least one space before the code.