What is the difference between a structure and a union?

Options
- We can define functions within structures but not within a union
- We can define functions within union but not within a structure
- The way memory is allocated
- They have no difference.


CORRECT ANSWER : The way memory is allocated

Discussion Board
Difference between structure & union

-The major difference between the structure & union is the way of allocated memory.

-The total memory required to store a structure variable is equal to the sum of the size of all the data members.
e.g struct stud
{
int rno; //int =2 bytes
char g; //char=1 byte
float marks; // float= 4 bytes
}s;

In above example, 7 bytes(2+1+4) are required to store structured variable 's'.

-In union, the total memory space allocated equal to the member of largest size.

e.g. union stud
{
int rno;
char g;
float marks;
}s;

Here, the largest size is 4 bytes. Hence, the total memory required to store union variable is 4 bytes. All other members, share the same memory space. We can use only one member at a time in union.

Sapna 02-15-2017 04:40 AM

union and structure

the difference between union and structure is memory allocated
and all elements in union are stored at same place
and all elements in structure are separated memory allocation this main difference

simhachalam amara 08-26-2014 02:24 AM

Union and Structure in C

- The difference between structure and union is in the way the memory is allocated.
- All elements in a Union are stored at the same place.
- With structure, there's a separate memory location for each element.

Aparna 07-5-2013 06:06 AM

Write your comments


Enter the code shown above:

(Note: If you cannot read the numbers in the above image, reload the page to generate a new one.)


Advertisement