Concept of Structure and Union: defining, declaring, initializing, accessing

Struct अलग-अलग types को एक record में बाँधता है (struct student: roll, name, marks, grade), dot operator से access होता; एक union देखने में बिलकुल वैसा है पर उसके members एक ही memory space साझा करते हैं, एक बार में एक।

12 min read · 11 cards · 3 checks

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


Theory

जो finale subject ने वादा किया

Marks program का data देखिए, सामान की तरह बिखरा: roll numbers एक array में, names एक char grid में, marks दूसरे array में, grades चौथे में। Student 17, चार index-17s का एक संयोग है। एक array sort कीजिए और बाकी भूल जाइए, तो Riya को Aman का grade मिल जाता है।

हक़ीक़त जो चाहती है वह एक record है: यह roll, यह name, ये marks, साथ-साथ सफ़र करते।

C का जवाब है struct, इस subject का आख़िरी और सबसे job-relevant idea।

Theory

हर student की एक file

College office सबके rolls का एक register, सबके names का दूसरा, marks का तीसरा नहीं रखता। वह हर student की एक FILE रखता है जिसमें हर detail अंदर होती है, और ऐसी files की एक अलमारी। Struct वही file है (मिली-जुली सामग्री, एक cover); structs का array वही अलमारी है। Array ने कहा था "60 boxes, एक ही type"; struct कहता है "एक bundle, अलग types"। दोनों मिलकर कुछ भी model कर सकते हैं।

Theory

Define, declare, initialize, access

Type define कीजिए (एक template, अभी कोई memory नहीं):

struct student {

int roll;

char name[20];

int marks;

char grade;

};

Declare variables (अब memory): struct student s1;

Initialize: struct student s1 = {42, "Riya", 78, 'A'};

Dot से access: s1.marks = 92; printf("%s", s1.name); और name member के लिए, strcpy(s1.name, "Riya") (arrays = से इनकार करते हैं, हमेशा की तरह)।

Practical

Marks program, आख़िरी रूप

#include <stdio.h>

struct student {
    int  roll;
    char name[20];
    int  marks;
    char grade;
};

int main() {
    struct student klass[3] = {
        {1, "Riya", 78, 'A'},
        {2, "Aman", 55, 'B'},
        {3, "Zoya", 91, 'A'}
    };
    int i;
    for (i = 0; i < 3; i++)
        printf("%d %-6s %3d  %c\n", klass[i].roll,
               klass[i].name, klass[i].marks, klass[i].grade);
    return 0;
}

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

Quiz

struct student { int roll; char name[20]; int marks; char grade; }; सिर्फ यह definition कितनी memory reserve करती है?

  1. कोई नहीं: definition एक template है; memory variable declaration के साथ आती है
  2. तुरंत 29 bytes
  3. semicolon के लिए एक byte
  4. name array के लिए 20 bytes
Show the answer

कोई नहीं: definition एक template है; memory variable declaration के साथ आती है

Struct define करना C को एक नया TYPE सिखाता है, एक stencil, और stencils कुछ नहीं घेरते। Memory हर VARIABLE के हिसाब से allocate होती है: struct student s1; असली bytes काटता है (मोटे तौर पर members का जोड़, साथ में alignment padding)। "क्या definition memory allocate करती है?" theory papers का पक्का सवाल है, और जवाब एक साफ़ ना है।

Think first

किसी को खोए बिना sort कीजिए

bubble sort का temp वाला swap याद है? struct की दुनिया में, class को marks से ऐसे कैसे sort करें कि names और grades अपने marks के साथ चलें? Swap का temp क्या है?

Show the answer

पूरे records swap कीजिए: struct student temp = klass[j]; klass[j] = klass[j+1]; klass[j+1] = temp;, structs, arrays के उलट, = assignment की इजाज़त देते हैं, हर member एक साथ copy करते। Parallel-arrays वाला bug (Riya को Aman का grade) नामुमकिन हो जाता है: file एक इकाई की तरह चलती है। यह struct-sort, marks program की graduation परीक्षा है, और एक असली lab सवाल।

Theory

Union: वही syntax, साझा कमरा

Keyword बदलिए और कुछ जड़ से बदल जाता है:

union value {

int i;

float f;

char c;

};

एक union के सारे members एक ही memory block साझा करते हैं, सबसे बड़े member के हिसाब से sized। u.i = 65 लिखिए, और u.f तथा u.c उन्हीं bytes को देख रहे हैं: u.f = 2.5 लिखिए और u.i overwrite हो जाता है, garbage। एक drawer जिसकी सामग्री का label बदलता है: एक बार में एक member ज़िंदा। इसका मक़सद: memory बचाना जब value हमेशा कई types में से सिर्फ एक हो (embedded systems, variant records)।

Quiz

union value u; u.i = 100; u.f = 2.5; printf("%d", u.i); क्या print होता है?

  1. Garbage: u.f लिखने से वे साझा bytes overwrite हो गए जिनमें 100 था
  2. 100: हर member अपना storage रखता है
  3. 2: u.f, u.i में round हो जाता है
  4. 102: members जुड़ जाते हैं
Show the answer

Garbage: u.f लिखने से वे साझा bytes overwrite हो गए जिनमें 100 था

एक कमरा, एक निवासी: u.f अंदर आया और u.i का 100 ढह गया; अब u.i पढ़ना float bytes को एक int के रूप में दोबारा पढ़ता है, garbage। एक STRUCT में दोनों members के अपने कमरे होते और 100 बच जाता। यह overwrite trace THE union exam सवाल है, और struct-बनाम-union का सबसे तीखा प्रदर्शन।

Watch out

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

struct definition के closing brace के बाद semicolon भूलना: एक मशहूर उलझाने वाला error cascade। गलत चीज़ पर dot: klass.marks गलत है, klass[i].marks सही, array element ही struct है। name member पर =: s1.name = "Riya" नाकाम (array!), उसे strcpy कीजिए। Union में लापरवाही: सिर्फ आख़िरी-लिखा member पढ़ने लायक है; दूसरा पढ़ना garbage है, error नहीं।

Theory

आप database से एक keyword दूर हैं

struct student को देखिए और आँखें सिकोड़िए: roll, name, marks, grade... वह एक database ROW है, और klass[60] एक TABLE है। BCA105 का SQL आपको यही idea एक query language के साथ थमाएगा, और आगे आप जो भी backend job करेंगे वह ऐसे ही records घुमाती है। अगला lesson subject को बंद करता है: struct बनाम union, औपचारिक रूप से, उस comparison table में जो exams माँगते हैं।

Summary

Key takeaways

  • Struct अलग types को एक record type में बाँधता है; definition कुछ allocate नहीं करती।
  • Memory के लिए declare कीजिए, {} से initialize, dot से members access: s1.marks।
  • structs के arrays (klass[60]) marks program का असली रूप हैं; structs पूरे-record copy के लिए = की इजाज़त देते हैं।
  • char-array members के लिए strcpy; उन पर कभी = नहीं।
  • एक union के members एक memory block साझा करते हैं (सबसे बड़े का size): एक बार में एक ज़िंदा।
  • एक union member लिखने से बाकी corrupt होते हैं, struct के members अलग रहते हैं।
  • याद रखने का hook: हर student की एक file; union एक drawer है, नए label वाला।

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

Concept of Structure and Union: defining, declaring, initializing, accessing · Computer Programming and Programming Methodology (CPPM) · Gri-Learn