ProAnswers.org

What is difference between structure and union in C?

1 Like

The difference between structure and union

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

  • 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

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.

Structure:

  1. It allocates memory equal to sum of memory allocated to its each individual member.
  2. each member have their own memory space.
  3. structure can not be implemented in shred memory.
  4. it has less Ambiguity.
  5. Self referential structure can be implemented in data structure.
  6. All members if structure can be accessed at a time.

You can watch Structure Video here

Union:

  1. It allocates piece of memory that is Large enough to hold the Largest variable of type in union.
  2. one block is used by all the members of union.
  3. Union is the Best environment where memory is shared.
  4. as memory is shared, Ambiguity is more in union.
  5. self ref. union can not be implemented.
  6. only one member is accessed at a time.

You can watch Union Video here

Difference between structure and union in Table format

Structure Union
1. Access Members
We can access all the members of structure at anytime. Only one member of union can be accessed at anytime.
2. Memory Allocation
Memory is allocated for all variables. Allocates memory for variable which variable require more memory.
3. Initialization
All members of structure can be initialized. Only the first member of a union can be initialized.
4. Keyword
‘struct’ keyword is used to declare structure. ‘union’ keyword is used to declare union.
5. Syntax

Structure

struct struct_name
{
    structure element 1;
    structure element 2;
	----------
	----------
    structure element n;
}struct_var_nm;

Union

union union_name
{
    union element 1;
    union element 2;
	----------
	----------
    union element n;
}union_var_nm;

6. Example

Structure

struct item_mst
{
    int rno;
    char nm[50];
}it;

Union

union item_mst
{
    int rno;
    char nm[50];
}it;

In union members share memory and the memory allocated is equal to the memory allocation of data member which occupies maximum space.
In structure data members do not share memory they have their own memory and are contiguously stored.The memory allocated is equal to the sum of byte allocation of individual members.

structure:

  • We can access any member at any given time without data corruption.
  • Squeezes more memory space.

Union:

  • Occupies less memory.
  • We can access only one union member at a time without data loss,since the members share memory.

Difference are:

  • 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.
  • 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.
  • union is best in the environment where memory is less as it shares the memory allocated.But structure can not implemented in shared memory.
  • As memory is shared,ambiguity are more in union,but less in structure.
  • self referential union can not be implemented in any data structure ,but self referential structure can be implemented.

Structures and Unions are used to store members of different data types.

STRUCTURE UNION

a)Declaration:

struct

{

data type member1;

data type member2;

};

a)Declaration:

union

{

data type member1;

data type member2;

};

b)Every structure member is allocated memory when a structure variable is defined.

Example:

b)The memory equivalent to the largest item is allocated commonly for all members.

Example:

struct emp {

char name[5];

int age;

float sal;

};

struct emp e1;

Memory allocated for structure is 1+2+4=7 bytes. 1

byte for name, 2 bytes for age and 4 bytes for sal.

union emp1 {

char name[5];

int age;

float sal;

};

union emp1 e2;

Memory allocated to a union is equal to size of the

largest member. In this case, float is the largest-sized

data type. Hence memory allocated to this union is 4

bytes.

c)All structure variables can be initialized at a time

struct st {

int a;

float b;

};

struct st s = { .a=4, .b=10.5 };

Structure is used when all members are to be

independently used in a program.

c)Only one union member can be initialized at a time

union un {

int a;

float b;

};

union un un1 = { .a=10 };

Union is used when members of it are not required to be

accessed at the same time.

Difference between Structure and Union

  • In Structure, all the data members are stored in separate memory locations whereas in Union all members share same memory location.
  • The memory occupied by a structure variable is equal to sum of the memory of all structure members whereas the memory occupied by a union variable is equal to the storage space needed by the largest data member of the union.
  • At a time, a union variable can contain only one active member, whereas a structure variable contains all member.