Union of Structure in C Language (C Language में Union of Structure)
Definition
A union that contains a structure variable as one of its data members is called a Union of Structure. In C language, a structure can be declared as a member of a union.
In the following example, course is a union that contains a structure variable s of type struct student as one of its members.
जिस Union में Structure Type के Variable को Data Member के रूप में Declare किया जाता है, उसे Union of Structure कहते हैं। C Language में किसी Structure को Union के Member के रूप में Declare किया जा सकता है।
निम्न उदाहरण में course नामक Union में struct student Type का Variable s एक Member के रूप में Declare किया गया है।
Example
struct student{int roll_no;char name[20];char gender;float fees;};union course{int courseid;char course_name[20];struct student s;int strength;};union course c;
Explanation
In the above example:
studentis a structure containing four members:roll_no,name,genderandfees.courseis a union containing four members:courseid,course_name,sandstrength.sis a structure variable declared as a member of the union.cis a variable of the unioncourse.
उपरोक्त उदाहरण में:
studentएक Structure है जिसमें चार Members हैं:roll_no,name,genderऔरfees।courseएक Union है जिसमें चार Members हैं:courseid,course_name,sऔरstrength।s, Union के अंदर Declare किया गया Structure Variable है।c,courseUnion का Variable है।
Memory Allocation
The size of a union is generally equal to the size of its largest member, including any padding required by the compiler.
In this example, the size of the union depends on the size of struct student and the compiler's memory alignment rules. Therefore, we should not always assume that the union occupies exactly 27 bytes.
Union का Size सामान्यतः उसके सबसे बड़े Member के Size के बराबर होता है। इसमें Compiler द्वारा आवश्यक Padding भी शामिल हो सकती है।
इस उदाहरण में Union का Size struct student के Size और Compiler के Memory Alignment Rules पर निर्भर करता है। इसलिए यह मानना सही नहीं है कि Union हमेशा 27 Bytes Memory लेगा।
Important: All union members share the same memory location. Therefore, only one member's stored value should be treated as the currently active value at a time.
Comments
Post a Comment