Structures and Unions in C++ Notes | Structure, Union, Difference & Applications | Basic Computer Engineering | RGPV BTech First Year
Structures and Unions in C++ Notes | Structure, Union, Difference & Applications | Basic Computer Engineering | RGPV BTech First Year
Structures and Unions in C++
Structure तथा Union C++ Programming के महत्वपूर्ण User Defined Data Types हैं। इनका उपयोग विभिन्न प्रकार (Different Data Types) के Data को एक ही नाम के अंतर्गत Store करने के लिए किया जाता है। Structure तथा Union की सहायता से Complex Data को व्यवस्थित (Organized) रूप में Store एवं Manage किया जा सकता है।
यदि किसी Student की Information जैसे Roll Number, Name, Percentage तथा Branch को Store करना हो, तो अलग-अलग Variables बनाने के बजाय Structure का उपयोग करना अधिक उपयुक्त होता है। इसी प्रकार Memory Saving की आवश्यकता होने पर Union का उपयोग किया जाता है।
RGPV B.Tech First Year के Basic Computer Engineering Subject में Structures and Unions अत्यंत महत्वपूर्ण Topic है। University Examination, Practical तथा Viva में Structure Declaration, Structure Initialization, Nested Structure, Union तथा Structure और Union के Difference से संबंधित प्रश्न नियमित रूप से पूछे जाते हैं।
↓
Structure / Union
↓
Single User Defined Type
↓
Easy Data Management
Introduction to Structure
Programming में कई बार ऐसे Data को Store करना पड़ता है जिनका Data Type अलग-अलग होता है। उदाहरण के लिए किसी Student के Record में Name (Character Array), Roll Number (Integer), Percentage (Float) तथा Grade (Character) शामिल हो सकते हैं। ऐसे विभिन्न प्रकार के Data को एक ही Group में Store करने के लिए Structure का उपयोग किया जाता है।
Structure Programmer द्वारा बनाया गया एक User Defined Data Type है जो अलग-अलग प्रकार के Variables को एक Logical Unit में संगठित करता है। इससे Program अधिक Readable तथा Maintainable बनता है।
What is Structure?
Structure एक User Defined Data Type है जिसके माध्यम से विभिन्न Data Types के Variables को एक ही नाम के अंतर्गत Group किया जाता है। Structure का प्रत्येक Member अपनी अलग Memory प्राप्त करता है तथा सभी Members एक साथ उपयोग किए जा सकते हैं।
ऊपर दिए गए उदाहरण में Student नाम का Structure बनाया गया है जिसमें तीन अलग-अलग Data Types के Members शामिल हैं।
Need of Structure
Structure का उपयोग तब किया जाता है जब किसी वास्तविक वस्तु (Real World Entity) से संबंधित अनेक प्रकार के Data को एक साथ Store करना हो।
- Related Data को एक Group में Store करने के लिए।
- Student, Employee एवं Product Records बनाने के लिए।
- Large Programs में Data Management आसान बनाने के लिए।
- Program की Readability बढ़ाने के लिए।
- Complex Data को व्यवस्थित रूप में Store करने के लिए।
- Database Records के समान Data Structure तैयार करने के लिए।
- Real World Applications को Efficient बनाने के लिए।
Advantages of Structure
| Advantage | Description |
|---|---|
| Organized Data | संबंधित Data को एक ही स्थान पर Store किया जा सकता है। |
| Easy Maintenance | Program को Update करना आसान हो जाता है। |
| Better Readability | Program अधिक सरल एवं समझने योग्य बनता है। |
| Code Reusability | एक ही Structure के अनेक Objects बनाए जा सकते हैं। |
| Real World Modeling | Student, Employee, Product आदि का Data आसानी से Store किया जा सकता है। |
Characteristics of Structure
Structure की कुछ महत्वपूर्ण विशेषताएँ (Characteristics) होती हैं जो इसे सामान्य Variables से अलग बनाती हैं। Structure विभिन्न Data Types के Members को एक ही User Defined Data Type के अंतर्गत व्यवस्थित करता है।
- Structure एक User Defined Data Type है।
- इसमें विभिन्न (Different) Data Types के Members हो सकते हैं।
- Structure का प्रत्येक Member अपनी अलग Memory प्राप्त करता है।
- Members को Dot (.) Operator द्वारा Access किया जाता है।
- एक Structure के अनेक Objects बनाए जा सकते हैं।
- Structures को Function में Pass किया जा सकता है।
- Nested Structure तथा Array of Structures बनाए जा सकते हैं।
Structure Declaration
Structure बनाने की प्रक्रिया को Structure Declaration कहा जाता है। इसमें struct Keyword का उपयोग किया जाता है तथा उसके अंदर Members Declare किए जाते हैं।
Syntax
Example
ऊपर दिए गए उदाहरण में Student नाम का Structure Declare किया गया है जिसमें चार Members शामिल हैं।
Creating Structure Variables
Structure Declare करने के बाद उसके Variables (Objects) बनाए जाते हैं। प्रत्येक Variable Structure के सभी Members के लिए अलग-अलग Memory Allocate करता है।
यहाँ s1, s2 तथा s3 तीन अलग-अलग Structure Variables हैं।
Memory Representation of Structure
Structure का प्रत्येक Member अलग Memory Location प्राप्त करता है। Structure की कुल Memory सामान्यतः उसके सभी Members की Memory का योग होती है (Padding को छोड़कर)।
| Member | Data Type | Memory |
|---|---|---|
| roll | int | 4 Bytes |
| name | char[30] | 30 Bytes |
| percentage | float | 4 Bytes |
| grade | char | 1 Byte |
वास्तविक Memory Size Compiler एवं Padding Rules पर निर्भर कर सकती है।
Structure Object Representation
↓
s1
↓
roll
name
percentage
grade
प्रत्येक Structure Object अपने सभी Members की अलग-अलग Copy रखता है। इसलिए एक Object में परिवर्तन करने से दूसरे Object पर कोई प्रभाव नहीं पड़ता।
Structure Initialization
Structure Initialization का अर्थ है Structure Variable बनाते समय उसके Members को प्रारंभिक (Initial) Values प्रदान करना। Initialization करने से Program अधिक सरल तथा Readable बन जाता है।
Syntax
Example
ऊपर दिए गए उदाहरण में Structure Variable s1 के सभी Members को Declaration के समय ही Initialize किया गया है।
Accessing Structure Members
Structure के Members को Access करने के लिए Dot (.) Operator का उपयोग किया जाता है। प्रत्येक Member को Structure Variable के साथ Dot Operator लगाकर Access किया जाता है।
Syntax
Example
Output
Reading Data into Structure
Keyboard से Structure Members में Data Input करने के लिए Dot Operator के साथ cin का उपयोग किया जाता है।
Character Array Member (Name) के लिए cin.getline() अथवा cin का उपयोग किया जा सकता है।
Displaying Structure Data
Structure में Store किए गए Data को Display करने के लिए प्रत्येक Member को Dot Operator के माध्यम से Access किया जाता है।
Complete Program using Structure
Output
Advantages of Using Structure
- Different Data Types को एक ही Object में Store किया जा सकता है।
- Real World Entities को आसानी से Represent किया जा सकता है।
- Program अधिक Organized एवं Readable बनता है।
- Maintenance तथा Modification सरल हो जाती है।
- Large Projects में Data Management आसान होता है।
- Database Records के समान Data Store किया जा सकता है।
Array of Structures
जब एक ही प्रकार के अनेक Structure Variables की आवश्यकता होती है, तब Array of Structures का उपयोग किया जाता है। इसमें Array का प्रत्येक Element एक पूर्ण Structure Object होता है।
Syntax
ऊपर दिए गए उदाहरण में s नाम का Array बनाया गया है जिसमें पाँच Student Structure Objects Store किए जा सकते हैं।
Example
Output
Nested Structure
जब किसी Structure के अंदर दूसरा Structure Member के रूप में Declare किया जाता है, तब उसे Nested Structure कहा जाता है। इसका उपयोग Complex Data को व्यवस्थित रूप में Store करने के लिए किया जाता है।
Example
यहाँ Address Structure को Student Structure के अंदर उपयोग किया गया है।
Accessing Nested Structure Members
Nested Structure के Members को Access करने के लिए Dot (.) Operator का कई बार उपयोग किया जाता है।
ऊपर दिए गए उदाहरण में s1.add.city द्वारा Nested Structure के Member को Access किया गया है।
Passing Structure to Function
Structure Variable को Function में Argument के रूप में Pass किया जा सकता है। इससे Function Structure के Members पर Processing कर सकता है।
Output
Structure and Function Return Type
C++ में Function किसी Structure Object को Return भी कर सकता है। इससे Complex Data को एक साथ Return करना संभव हो जाता है।
Structure vs Class (Basic Introduction)
C++ में Structure तथा Class दोनों User Defined Data Types हैं। दोनों लगभग समान कार्य करते हैं, लेकिन उनका Default Access Modifier अलग होता है।
| Structure | Class |
|---|---|
| Default Access Modifier Public होता है। | Default Access Modifier Private होता है। |
| मुख्यतः Data Store करने के लिए उपयोग किया जाता है। | Data तथा Member Functions दोनों को Represent करता है। |
| सरल Data Structures के लिए उपयुक्त। | Object Oriented Programming में उपयोगी। |
Introduction to Union
Union भी एक User Defined Data Type है, लेकिन Structure से भिन्न इसमें सभी Members एक ही Memory Location को Share करते हैं। इसलिए Union की कुल Memory उसके सबसे बड़े Member के बराबर होती है।
जब किसी समय केवल एक ही Member का उपयोग करना हो तथा Memory बचाना उद्देश्य हो, तब Union का उपयोग किया जाता है।
Union Declaration
Union Declare करने के लिए union Keyword का उपयोग किया जाता है। इसकी Syntax Structure के समान होती है, लेकिन इसमें सभी Members एक ही Memory Location को Share करते हैं।
Syntax
Example
ऊपर दिए गए उदाहरण में Data नाम का Union बनाया गया है जिसमें तीन Members हैं, लेकिन किसी भी समय केवल एक Member का उपयोग किया जा सकता है।
Creating Union Variable
Union Declare करने के बाद उसके Variables बनाए जाते हैं। प्रत्येक Union Variable सभी Members के लिए केवल एक Common Memory Area Allocate करता है।
Accessing Union Members
Union के Members को भी Structure की तरह Dot (.) Operator द्वारा Access किया जाता है।
यदि बाद में किसी दूसरे Member में Value Store की जाती है, तो पहले Member की Value Overwrite हो जाती है क्योंकि सभी Members एक ही Memory का उपयोग करते हैं।
Memory Allocation in Union
Union की सबसे महत्वपूर्ण विशेषता यह है कि इसके सभी Members एक ही Memory Location को Share करते हैं। इसलिए Union की कुल Memory उसके सबसे बड़े Member के बराबर होती है।
| Member | Size (Example) |
|---|---|
| int | 4 Bytes |
| float | 4 Bytes |
| char | 1 Byte |
यदि ऊपर दिए गए तीन Members वाला Union बनाया जाए, तो Union का Size सामान्यतः 4 Bytes होगा क्योंकि सबसे बड़ा Member 4 Bytes का है।
Difference between Structure and Union
| Structure | Union |
|---|---|
| प्रत्येक Member के लिए अलग Memory Allocate होती है। | सभी Members एक ही Memory Share करते हैं। |
| सभी Members का एक साथ उपयोग किया जा सकता है। | एक समय में केवल एक Member Valid रहता है। |
| Memory अधिक उपयोग होती है। | Memory कम उपयोग होती है। |
| Complex Records Store करने के लिए उपयुक्त। | Memory Optimization के लिए उपयुक्त। |
| Student, Employee, Product Records | Embedded Systems, Device Drivers |
Program using Union
Output
ध्यान दें कि दूसरे Member में Value Store करने पर पहले Member की Value सुरक्षित नहीं रहती क्योंकि दोनों एक ही Memory Location का उपयोग करते हैं।
Applications of Structure
Structure का उपयोग उन सभी Applications में किया जाता है जहाँ विभिन्न प्रकार (Different Data Types) के संबंधित Data को एक साथ Store करना आवश्यक होता है। Real World Applications में Structure अत्यंत उपयोगी सिद्ध होता है।
| Application | Purpose |
|---|---|
| Student Management System | Student Records Store करना। |
| Employee Management | Employee Information Store करना। |
| Hospital Management | Patient Records Maintain करना। |
| Banking System | Customer Details Store करना। |
| Library Management | Book Records Store करना। |
| Inventory System | Product Details Maintain करना। |
Applications of Union
Union का मुख्य उद्देश्य Memory बचाना होता है। इसलिए इसका उपयोग Embedded Systems तथा उन Applications में किया जाता है जहाँ Memory Limited होती है।
- Embedded Systems
- Device Drivers
- Microcontroller Programming
- Compiler Design
- Communication Protocols
- Operating Systems
- Hardware Interface Programming
Advantages of Structure
- Different Data Types को एक Group में Store किया जा सकता है।
- Program अधिक Organized बनता है।
- Real World Objects को Represent करना आसान होता है।
- Code Reusability बढ़ती है।
- Maintenance आसान हो जाती है।
- Functions के साथ Efficient Data Passing संभव होती है।
- Large Applications में Data Management सरल हो जाता है।
Advantages of Union
- Memory की बचत होती है।
- Large Data को कम Memory में Store किया जा सकता है।
- Embedded Applications के लिए उपयुक्त।
- Low Memory Systems में Efficient होता है।
- Memory Optimization प्रदान करता है।
Limitations of Structure and Union
| Structure | Union |
|---|---|
| Memory अधिक उपयोग करता है। | एक समय में केवल एक Member Valid रहता है। |
| Large Records में Memory Consumption बढ़ सकती है। | गलत Member Access करने पर Data Corruption हो सकता है। |
| Padding के कारण Memory Size बढ़ सकती है। | Programming अपेक्षाकृत कठिन हो सकती है। |
Summary
| Topic | Key Point |
|---|---|
| Structure | Different Data Types को Group करता है। |
| Union | Members एक ही Memory Share करते हैं। |
| Structure Variable | प्रत्येक Object की अलग Memory होती है। |
| Union Variable | सभी Members Common Memory का उपयोग करते हैं। |
| Nested Structure | Structure के अंदर दूसरा Structure। |
| Array of Structures | एक ही प्रकार के अनेक Structure Objects। |
Important Viva Questions
- Structure क्या है?
- Union क्या है?
- Structure तथा Union में क्या अंतर है?
- Structure Declaration कैसे करते हैं?
- Structure Initialization क्या है?
- Nested Structure क्या होता है?
- Array of Structures क्या है?
- Union Memory कैसे Allocate करता है?
- Structure तथा Class में क्या अंतर है?
- Union का उपयोग कहाँ किया जाता है?
University Examination Important Questions
- Structure की परिभाषा लिखिए तथा उसके Advantages समझाइए।
- Structure Declaration एवं Initialization उदाहरण सहित समझाइए।
- Array of Structures तथा Nested Structure को उदाहरण सहित समझाइए।
- Passing Structure to Function समझाइए।
- Union क्या है? इसकी आवश्यकता एवं Advantages लिखिए।
- Structure तथा Union में अंतर लिखिए।
- Union की Memory Allocation प्रक्रिया समझाइए।
- Structure एवं Class में अंतर लिखिए।
- Structure एवं Union के Applications लिखिए।
- Structure एवं Union पर Short Notes लिखिए।
Conclusion
Structures and Unions C++ Programming के अत्यंत महत्वपूर्ण User Defined Data Types हैं। Structure विभिन्न प्रकार के Data को एक साथ व्यवस्थित रूप में Store करने के लिए उपयोग किया जाता है, जबकि Union Memory Optimization के लिए उपयोगी होता है। Student Records, Employee Management, Banking Systems तथा Embedded Systems जैसी वास्तविक Applications में इनका व्यापक उपयोग किया जाता है। RGPV B.Tech विद्यार्थियों के लिए Structure Declaration, Initialization, Nested Structure, Array of Structures, Union तथा Structure और Union के अंतर को समझना परीक्षा तथा Practical दोनों की दृष्टि से अत्यंत महत्वपूर्ण है।
Related Articles
Arrays of Objects in C++ Notes | Object Array, Declaration, Initialization & Examples | Basic Computer Engineering | RGPV BTech First Year
Arrays ...
Read More →Object as Function Argument & Returning Object from Function in C++ Notes | Pass Object to Function, Return Object & Examples | Basic Computer Engineering | RGPV BTech First Year
Object ...
Read More →Pointers to Objects in C++ Notes | Object Pointer, Accessing Members using Pointer & Examples | Basic Computer Engineering | RGPV BTech First Year
Pointer...
Read More →Dynamic Memory Allocation in C++ Notes | new & delete Operators, Heap Memory, Dynamic Objects & Examples | Basic Computer Engineering | RGPV BTech First Year
Dynamic...
Read More →this Pointer in C++ Notes | this Keyword, Uses, Advantages & Examples | Basic Computer Engineering | RGPV BTech First Year
this Po...
Read More →