C के Unions
In this page:
union UnionName {
data_type member1;
data_type member2;
};
union UnionName variable_name;
variable_name.member1 = value;
Union क्या है?
यह shared-memory design unions को structures से कहीं ज़्यादा memory-efficient बनाता है जब आपको किसी भी समय कई possible values में से सिर्फ एक चाहिए हो -- उदाहरण के लिए, एक value represent करना जो एक int, एक float, या एक string हो सकती है, लेकिन एक साथ एक से ज़्यादा कभी नहीं।
उदाहरण: What is a Union?
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
float f;
};
// Program execution starts in main()
int main() {
union Data d;
d.i = 5;
// Print formatted text to the screen
printf("%d", d.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
एक Union Define करना
यह syntactically एक structure define करने जैसा ही है, लेकिन compiler का memory layout fundamentally अलग है: हर member sequentially lay out होने की बजाय same base address से शुरू होता है।
उदाहरण: Defining a Union
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Value {
int i;
char c;
};
// Program execution starts in main()
int main() {
union Value v;
v.i = 65;
// Print formatted text to the screen
printf("%d", v.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unions में Memory Allocation
अगर आपके union में एक int (4 bytes) और एक double (8 bytes) है, union का total size 8 bytes है -- इसके सबसे बड़े member के लिए exactly काफी, क्योंकि सारे members stacked होने की बजाय same memory region overlap करते हैं।
उदाहरण: Memory Allocation in Unions
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
double d;
};
// Program execution starts in main()
int main() {
// Print formatted text to the screen
printf("%zu", sizeof(union Data));
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Union Members Access करना
एक member में लिखना और फिर एक अलग member पढ़ना same raw bytes को एक अलग type की तरह reinterpret करता है, जो कभी-कभी जानबूझकर किया जाता है (जिसे 'type punning' कहते हैं) लेकिन accidentally होने पर आमतौर पर एक bug है।
उदाहरण: Accessing Union Members
// Include standard input/output (printf, scanf)
#include <stdio.h>
union Data {
int i;
float f;
};
// Program execution starts in main()
int main() {
union Data d;
d.i = 10;
d.f = 3.5f;
// Print formatted text to the screen
printf("%.1f", d.f);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
Unions बनाम Structures
एक structure चुनें जब आपको एक साथ कई related values store करनी हों (जैसे एक point का x और y), और एक union जब आपको एक value represent करनी हो जो context के आधार पर कई अलग types में से एक हो सकती है (जैसे एक variant/tagged data type)।
उदाहरण: Unions vs. Structures
// Include standard input/output (printf, scanf)
#include <stdio.h>
// Define a structure type named Point
struct Point { int x, y; };
union Value { int i; float f; };
// Program execution starts in main()
int main() {
struct Point p = {1, 2};
union Value v;
v.i = 5;
// Print formatted text to the screen
printf("%d %d %d", p.x, p.y, v.i);
// Return 0 to signal that the program finished successfully
return 0;
}
Login to try C/C++/Java/PHP code in the editor
- आखिरी बार लिखे गए के अलावा एक union member पढ़ना, जो unreliable results देता है।
- यह मान लेना कि सारे union members एक साथ values रखते हैं।
- यह मान लेना कि एक union का size इसके members का sum है, जब यह सबसे बड़े का size है।
Chapter Quiz — Complete all 7 topics to unlock
0/7 topics done
Complete these topics first: