https://www.rakeshmgs.in/search/label/Template
https://www.rakeshmgs.in

RakeshMgs

Unions and Structure Storage Comparison in C Programming in Hindi Notes

Updated:

Unions in C Language

Unions वैचारिक रूप से structures के समान हैं। Union की declare / define करने का syntax भी एक structure के समान है। storage के संदर्भ में केवल अंतर हैं। structure में प्रत्येक member का अपना storage स्थान होता है, जबकि unions के सभी member each on memory का उपयोग करते हैं जो इसके सबसे largest data member के आकार के बराबर होता है।

Unions in C Language



इसका तात्पर्य यह है कि यद्यपि एक unions में विभिन्न प्रकार के कई member हो सकते हैं, यह एक ही समय में all member को नहीं संभाल सकता है। union keyword का उपयोग करके एक union घोषित किया जाता है।

	
	
union item { int m; float x; char c; }It1;

यह टाइप union आइटम का एक variable It1 declares करता है। इस union में एक अलग data type के साथ तीन सदस्य होते हैं। हालांकि, उनमें से केवल एक समय में इस्तेमाल किया जा सकता है। यह इस तथ्य के कारण है कि सभी union variable के लिए केवल एक स्थान आवंटित किया गया है, चाहे उनका आकार कितना भी हो। compile storage को आवंटित करता है जो कि union में largest variable type धारण करने के लिए पर्याप्त है।

member x के ऊपर declared union में 4 बाइट्स की आवश्यकता होती है जो 16-बिट machine के लिए members में largest होता है। union के अन्य समान memory address साझा करेंगे।

Accessing a Union Member

	
	
union test { int a; float b; char c; }t; t.a; //to access members of union t t.b; t.c;

Time for an Example

	
	
#include <stdio.h> union test { int a; float b; char c; }; int main() { union item it; it.a = 12; it.b = 20.2; it.ch = 'z'; printf("%d\n", it.a); printf("%f\n", it.b); printf("%c\n", it.ch); return 0; }

output

-26426
20.1999
z

जैसा कि आप यहाँ देख सकते हैं, a और b का value corrupted हो गया है और केवल variabel c expected परिणाम को प्रिंट करता है। ऐसा इसलिए है, क्योंकि union में, मेमोरी को विभिन्न डेटा प्रकारों के बीच share किया जाता है। इसलिए, एकमात्र member जिसका value वर्तमान में store है, उसमें मेमोरी होगी।

उपरोक्त उदाहरण में, variable c का मान अंतिम पर संग्रहीत किया गया था, इसलिए अन्य variable का मान lost गया है।


आपको आर्टिकल कैसा लगा? अपनी राय अवश्य दें
Please don't Add spam links,
if you want backlinks from my blog contact me on rakeshmgs.in@gmail.com

🔒 Secure Session Protected

Unauthorized Inspection Detected

Security protection active hai.
Is page ko continue karne ke liye Developer Tools band karein.

Kripya Inspect / Console / Sources / Network panel close karein.

Protected Content • Secure View Enabled