Union in Hindi

in Hindi - Union

  • Union ये एक अलग-अलग data types का collection होता है |
  • अगर union का इस्तेमाल करना हो तो 'union' keyword का इस्तेमाल करते है |
  • Union ये structure के जैसा ही होता है |
  • Union में किये हुए हर variable के decaration को 'member' कहते है |
  • Union हर एक member के लिए अलग-अलग memory allocate नहीं करता है |
  • Union के members एक ही memory location को share करते है |
  • Union में जो member अपने size में बड़ा होता है, तो वो पूरे Union की size होती है |

Syntax Difference between Structure and Union Defintion

Syntax for Structure Definition
struct structure_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
};  
Syntax for Union Definition
struct union_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
};  

Syntax में सिर्फ struct और union keyword में फर्क है | Structure के members अलग-अलग memory allocate करते है | Union के members एक ही member की memory पर store होते है |


Example for Structure Definition
struct Employee{
    int emp_id;
    char emp_name[30];
}; 
Example for Union Definition
union Student{
    int stud_id;
    char stud_name[30];
};


Memory Representation



StructureUnion
Structure_nameEmployeeStudent
Member_nameemp_idemp_name[30]stud_id and stud_name[30]Location
Memory Address0x69fed40x69fed80x69fee8
Member Size4bytes30bytes4bytes30bytes
Total Size34bytes30bytes

Union Variable Declaration

Structure का variable declare करने के दो प्रकार है |
  1. जब Union की definition लिखी जाती तब वहा पर भी Structure variable को लिखा जाता है |
  2. Union के variable को main() function के अंदर भी किया जाता है |

Syntax for Union Variable outside of Union Definition

struct Union_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
}Union_variable(s);  

Example for Union Variable in main() Function

struct Employee{
    int emp_id;
    char emp_name[30];
    float salary;
}info;

Syntax for Union Variable in main() Function

union Union_name{  

    data_type member 1;  
    data_type member 2;  
    ------------------
    ------------------
    data_type memeber n;  
}Union_variable(s);
int main(){
	union Union_name Union_variable_name;
}		

Example for Union Variable in main() Function

union Employee{
    int emp_id;
    char emp_name[30];
    float salary;
};
int main(){
	union Employee info;
}	

Full Example for Union

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

union Employee {
    int emp_id;
    char emp_name[30];
    float salary;
}info;

int main(){

    info.emp_id = 34;
    strcpy( info.emp_name, "Raj Biradar");
    info.salary = 20000.00;

    cout<<"Employee id is : "<<info.emp_id<<endl;
    cout<<"Employee name is : "<<info.emp_name<<endl;
    cout<<"Employee salary is : "<<info.salary<<endl;

return 0;
}
Output :
Employee id is : 1184645120
Employee name is
Employee salary is : 20000

Union working with size(sizeof)

Union के member एक ही memory location में store होता है और पूरे Union का size जो सबसे बड़ा member होता है, वो size Union की होती है |

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

union Employee {
    int emp_id;
    char emp_name[20];
    float salary;
};
int main(){
	union Employee info;

    cout<<"Size of Employee id is : "<<sizeof(info.emp_id)<<" bytes."<<endl;  // size of emp_id
    cout<<"Size of Employee name : "<<sizeof(info.emp_name)<<" bytes."<<endl; // size of emp_name
    cout<<"Size of Employee salary is : "<<sizeof(info.salary)<<" bytes."<<endl; // size of salary
    cout<<"Size of Employee union : "<<sizeof(info)<<" bytes."<<endl; // size of Employee

return 0;
}
Ouutput :
Size of Employee id is : 4 bytes
Size of Employee name : 20 bytes
Size of Employee salary is : 4 bytes
Size of Employee union : 20 bytes

Union using Pointer

Union के Members को दो प्रकार से access किया जाता है |
  1. . (dot Operator)
  2. -> (pointer Operator)

Accessing Members using pointer Operator

Source Code :
#include <iostream.h>
#include <string.h>
using namespace std;

union Employee {
    int  emp_id;
    char  emp_name[30];
    float  salary;
};
int main(){
	union Employee info;
	union Employee *ptr;

	ptr = &info;

    ptr->emp_id = 34;
    strcpy( ptr->emp_name, "Raj Biradar");
    ptr->salary = 20000.00;

    cout<<"Employee id is : "<<ptr->emp_id<<endl;
    cout<<"Employee name is : "<<ptr->emp_name<<endl;
    cout<<"Employee salary is : "<<ptr->salary<<endl;

return 0;
}

< /pre>
Output :
Employee id is : 1184645120
Employee name is :
Employee salary is : 20000