Nested Structures in Hindi
/ BCA / Programming with C and CPP
Nested Structures in C Explained
Nested Structures in Hindi
What are Nested Structures in Hindi
C भाषा में जब हम एक structure के अंदर कोई दूसरा structure define करते हैं, तो उसे Nested Structure कहा जाता है। Nested का मतलब होता है – "अंदर छिपा हुआ"। जैसे एक इंसान के बारे में जानकारी लेने के लिए हमें उसका पता (Address), जन्मतिथि (Date of Birth), और नाम आदि चाहिए होता है। अगर हम Address को भी एक अलग structure में बनाकर main structure के अंदर use करें, तो यही Nested Structure कहलाता है।
Nested Structure की मदद से हम complex data को छोटे-छोटे manageable parts में बाँट सकते हैं और logically अच्छे से represent कर सकते हैं। यह beginners के लिए थोड़ा confusing हो सकता है, लेकिन अगर हम इसे एक practical example से समझें तो यह बहुत आसान हो जाएगा।
Syntax and Declaration of Nested Structures in Hindi
Nested Structure को declare करने के दो तरीके होते हैं:
- Structure को पहले अलग से declare करके, दूसरे structure में include करना।
- Structure को दूसरे structure के अंदर ही define करना।
पहले तरीके को "predefined nested structure" और दूसरे को "inline nested structure" कहा जा सकता है। नीचे दोनों तरीकों के उदाहरण दिए गए हैं।
Method 1: Predefined Nested Structure
struct Address {
char city[20];
char state[20];
int pincode;
};
struct Student {
int roll;
char name[20];
struct Address addr;
};
ऊपर दिए गए code में हमने पहले Address नाम का एक structure बनाया है, जिसमें city, state और pincode शामिल हैं। फिर हमने Student structure बनाया जिसमें roll, name और Address structure को शामिल किया गया है। यही Nested Structure है।
Method 2: Inline Nested Structure
struct Student {
int roll;
char name[20];
struct {
char city[20];
char state[20];
int pincode;
} addr;
};
इस तरीके में हम Address structure को Student structure के अंदर ही define करते हैं। इसका फायदा यह है कि इसे एक ही जगह define किया जा सकता है, लेकिन reuse नहीं किया जा सकता।
Accessing Nested Structure Members in Hindi
Nested Structure के members को access करने के लिए dot (.) operator का use किया जाता है। अगर हम structure variable बनाते हैं, तो सबसे पहले outer structure का नाम लिखते हैं, फिर dot operator लगाकर inner structure तक पहुंचते हैं और फिर उसमें से किसी member को access करते हैं।
मान लीजिए हमने Student structure का एक variable बनाया है:
struct Student s1;
अब अगर हमें उसका city print करना है तो हम लिखेंगे:
printf("%s", s1.addr.city);
यहां पर:
- s1 = structure variable
- addr = nested structure
- city = nested member
Complete Example of Nested Structure
#include <stdio.h>
#include <string.h>
struct Address {
char city[20];
char state[20];
int pincode;
};
struct Student {
int roll;
char name[20];
struct Address addr;
};
int main() {
struct Student s1;
s1.roll = 101;
strcpy(s1.name, "Aman");
strcpy(s1.addr.city, "Delhi");
strcpy(s1.addr.state, "Delhi");
s1.addr.pincode = 110001;
printf("Student Roll: %d\n", s1.roll);
printf("Student Name: %s\n", s1.name);
printf("City: %s\n", s1.addr.city);
printf("State: %s\n", s1.addr.state);
printf("Pincode: %d\n", s1.addr.pincode);
return 0;
}
ऊपर के program में हमने पहले एक Address structure बनाया जिसमें address से जुड़ी details हैं, और फिर Student structure बनाया जिसमें Address structure को include किया गया है। इस तरीके से हम एक object-oriented approach को C में implement कर सकते हैं।
Nested Structure क्यों उपयोगी है? (Importance of Nested Structure)
- यह large और complex data को logically organize करता है।
- Code को readable और manageable बनाता है।
- Data को hierarchical रूप से store करने की सुविधा देता है।
- Real-world problems को बेहतर represent करने में मदद करता है।
Important Points to Remember in Hindi
- Nested Structure का member access करने के लिए dot operator का use करना जरूरी है।
- अगर inner structure को बाहर define किया गया है तो उसे reuse किया जा सकता है।
- Inline nested structure को सिर्फ उसी structure के अंदर use किया जा सकता है।
- Nested Structure के use से program modular और organized बनता है।
Example Table Representation for Better Understanding
| Student Name | Roll Number | City | State | Pincode |
|---|---|---|---|---|
| Aman | 101 | Delhi | Delhi | 110001 |
इस table के माध्यम से हम nested structure का real use देख सकते हैं। हर row एक student के बारे में है और उसके अंदर एक अलग address block शामिल है।
Nested Structure का उपयोग अक्सर उस समय किया जाता है जब हमें structured और layered data store करना होता है। जैसे hospital management system, school record system, banking system आदि में यह बहुत उपयोगी होता है।
FAQs
s1.addr.city जहां s1 structure variable है।