Difference between structure and union

Structure बनाम union एक line में: struct हर member को उसकी अपनी memory देता है (size = जोड़) ताकि सब साथ रहें; union members को एक block साझा कराता है (size = सबसे बड़ा) ताकि सिर्फ आख़िरी-लिखा ज़िंदा रहे।

9 min read · 10 cards · 3 checks

Read in: English · हिन्दी · ગુજરાતી


Theory

एक keyword, दो सोच

पिछला lesson एक पहेली पर खत्म हुआ: struct और union एक जैसा syntax साझा करते हैं, braces, members, dots, फिर भी अलग प्रजातियों जैसे बर्ताव करते हैं।

Exams इस जोड़ी को बहुत पसंद करते हैं: "structure और union में अंतर बताइए" इतनी भरोसे से आता है कि seniors इसे मुफ़्त marks कहते हैं।

आज: comparison table, sizeof का सबूत, और वह एक वाक्य जो दोनों को हमेशा के लिए सुलझा देता है। वाक्य में एक hostel है।

Theory

अलग कमरे बनाम एक साझा कमरा

एक struct एक hostel wing है: हर member को अपना कमरा मिलता है, सब एक ही समय पर वहाँ रह सकते हैं, और wing का size कमरों का जोड़ है। एक union एक ही कमरा है जिसका nameplate घूमता है: जो अंदर आता है वह पिछले निवासी का सामान बाहर फेंक देता है। इसका size? बस इतना बड़ा कि सबसे बड़ा संभव निवासी समा जाए। इमारत का रूप वही; रहने की नीति उलटी।

At a glance

जो comparison exams माँगते हैं

पहलूStructureUnion
Memoryहर member की अपनी जगहसारे members एक block साझा करते हैं
Sizemembers का जोड़ (+ padding)सिर्फ सबसे बड़ा member
एक साथ ज़िंदासारे members एक साथसिर्फ आख़िरी-लिखा member
दूसरे पढ़नाहमेशा validGarbage (overwrite हुए bytes)
किसके लिए बेहतरRecords: student, employeeMemory-कम, कई में से एक value

Practical

sizeof का सबूत

#include <stdio.h>

struct sBox { int i; float f; char c; };
union  uBox { int i; float f; char c; };

int main() {
    printf("struct: %lu bytes\n", sizeof(struct sBox));
    printf("union : %lu bytes\n", sizeof(union uBox));
    return 0;
}

This example runs in Gri-Learn on the web, where you can edit it and see the output.

Think first

दोनों sizes का अनुमान लगाइए

एक ही तीन members: int (4), float (4), char (1)। Reveal से पहले दोनों sizeof नतीजे तर्क से निकालिए। (Hint: एक के लिए जोड़, दूसरे के लिए सबसे बड़ा, और C pad कर सकता है।)

Show the answer

struct: 4 + 4 + 1 = 9, ज़्यादातर compilers में pad होकर 12 (members 4-byte boundaries पर aligned)। union: सबसे बड़ा member 4 bytes का है, तो 4।

"members का जोड़, शायद pad किया हुआ" बनाम "सबसे बड़े member का size" लिखिए और sizeof सवाल पूरी तरह कमा लिया। 12-बनाम-4 का फ़र्क़ ही अंतर है, numbers में।

Quiz

struct के बजाय UNION कब सही चुनाव है?

  1. जब एक variable एक बार में कई types में से सिर्फ एक रखता है और memory क़ीमती है
  2. जब भी आप members तक तेज़ access चाहते हैं
  3. जब सारे members को साथ values रखनी हों
  4. कभी नहीं: unions पुराने पड़ चुके हैं
Show the answer

जब एक variable एक बार में कई types में से सिर्फ एक रखता है और memory क़ीमती है

Union का एक ही हुनर है किफ़ायत: एक sensor reading जो या तो int हो या float (कभी दोनों नहीं) को सिर्फ बड़ी जगह चाहिए, जोड़ नहीं, embedded systems और device programming में सोना। members को साथ चाहना (option C) ठीक struct का काम है। यहाँ के choice सवाल ठीक यही या/या फ़ैसला test करते हैं।

Quiz

struct sBox s; s.i = 10; s.f = 2.5; printf("%d", s.i); बनाम वही क्रम union uBox u पर। हर मामले में क्या print होता है?

  1. struct 10 print करता है; union garbage print करता है
  2. दोनों 10 print करते हैं
  3. दोनों garbage print करते हैं
  4. struct garbage print करता है; union 10 print करता है
Show the answer

struct 10 print करता है; union garbage print करता है

struct में, i और f अलग कमरों में रहते हैं: f लिखना i को छू नहीं सकता, तो 10 बच जाता है। union में वे एक कमरा हैं: u.f = 2.5 ने 10 को निकाल बाहर किया, और u.i पढ़ना float bytes को दोबारा पढ़ता है, garbage। यह जोड़ीदार trace "उदाहरण से अंतर दिखाइए" का सबसे साफ़ जवाब है।

Watch out

Marks कहाँ कटते हैं

padding बताए बिना struct के सटीक sizes लिखना: कहिए "members का जोड़, साथ में संभव alignment padding", ईमानदार पूरे-mark वाली phrasing। unions को तेज़ बताना: उनका गुण SIZE है, speed नहीं। union को कई values से initialize करना: सिर्फ पहला member initialize हो सकता है ({10} चलता है, {10, 2.5} नहीं)। और table वाले जवाब में, use-case वाली row हमेशा शामिल कीजिए, वहीं पाँचवाँ mark छिपा है।

Theory

Subject, पूरा

पीछे हटकर देखिए: marks program ने एक hard-coded line print करते शुरू किया, और student records के एक array के रूप में खत्म होता है जिसे वह पढ़, grade, sort और report कर सकता है, syllabus के हर construct का इस्तेमाल करते हुए। वह program ही आपका revision है: exam से पहले इसे याद से दोबारा बनाइए, और हर unit खुद को फिर से पढ़ा देता है। अगले semester, Python (BCA204) आपको मुस्कुराने पर मजबूर करेगा कि आप पहले से कितना समझते हैं।

Summary

Key takeaways

  • Struct: हर member की अलग memory; size = जोड़ (+ padding); सब साथ ज़िंदा।
  • Union: एक साझा block; size = सबसे बड़ा member; सिर्फ आख़िरी-लिखा member valid।
  • sizeof सबूत: {int, float, char} → struct ~12 (padded), union 4।
  • Union initialization सिर्फ पहले member को छूता है।
  • Records के लिए struct चुनिए (default); memory के दबाव में कई-में-से-एक values के लिए union।
  • याद रखने का hook: अलग कमरे बनाम घूमते nameplate वाला एक कमरा।

Study this properly

This page is the lesson to read. In Gri-Learn the same topic is a graded deck: the self-checks are scored and your weak topics are tracked. Free to start.

Start this topic

Already have an account? Sign in

More from Functions and Structure

Gri-Learn · syllabus-mapped B.C.A. lessons in English, Hindi and Gujarati

Difference between structure and union · Computer Programming and Programming Methodology (CPPM) · Gri-Learn