Pointers within Structure in Hindi
/ BCA / Programming with C and CPP
Pointers within Structure in Hindi
Pointers within Structure in Hindi
Understanding Pointers within Structure in Hindi
जब हम C language में Structure का उपयोग करते हैं, तो हम एक से अधिक data items को एक साथ group कर सकते हैं। लेकिन जब हमें किसी structure के अंदर dynamic memory allocation करनी होती है या किसी external data को refer करना होता है, तब हम structure के अंदर pointers का उपयोग करते हैं। इसे ही Pointers within Structure कहा जाता है।
Pointer एक ऐसा variable होता है जो किसी अन्य variable के memory address को store करता है। और जब हम pointer को structure के अंदर define करते हैं, तो हम structure को ज्यादा flexible और powerful बना सकते हैं।
उदाहरण के लिए, मान लीजिए हमें एक ऐसा student structure बनाना है जिसमें student का नाम dynamic रूप से store किया जाए। इसके लिए हमें pointer का उपयोग करना होगा, क्योंकि नाम की लंबाई fix नहीं होती।
struct Student {
char *name;
int age;
};
ऊपर दिए गए structure में name एक pointer है जो character type data को point करता है। इसका मतलब है कि यह किसी भी dynamic memory में allocate हुए string को point कर सकता है।
Declaration of Pointer Members in Structure in Hindi
Structure के अंदर pointer को declare करना बिलकुल सामान्य pointer declaration की तरह ही होता है, बस उसे structure के body में रखना होता है।
Syntax:
struct StructName {
dataType *pointerMember;
...other members...
};
Example:
struct Employee {
char *name;
int *salary;
};
यहाँ name एक character pointer है और salary एक integer pointer है। यह दोनों किसी अन्य memory location को point करेंगे।
Memory Allocation for Pointers within Structure in Hindi
Structure के अंदर declare किए गए pointers को उपयोग में लेने से पहले उनके लिए memory allocate करनी पड़ती है, नहीं तो यह undefined behavior देगा या segmentation fault भी आ सकता है। memory allocation के लिए हम malloc(), calloc() जैसे functions का प्रयोग करते हैं।
Example:
#include <stdio.h>
#include <stdlib.h>
struct Student {
char *name;
int *roll;
};
int main() {
struct Student s1;
s1.name = (char *)malloc(50 * sizeof(char));
s1.roll = (int *)malloc(sizeof(int));
strcpy(s1.name, "Rahul");
*s1.roll = 101;
printf("Name: %s\\n", s1.name);
printf("Roll: %d\\n", *s1.roll);
return 0;
}
ऊपर के उदाहरण में हमने malloc() का उपयोग कर के name और roll के लिए memory allocate की है और फिर उसमें data store किया है।
अगर आप memory allocate नहीं करेंगे और directly value assign करेंगे तो segmentation fault आने की संभावना होती है क्योंकि pointer अभी किसी valid memory को point नहीं कर रहा होता।
Accessing Pointer Data inside Structure in Hindi
Structure के अंदर pointer से data को access करने के लिए हम दो तरीकों का उपयोग कर सकते हैं:
- Dot (.) operator – जब structure का object direct होता है।
- Arrow (->) operator – जब structure का pointer use करते हैं।
Example with dot operator:
struct Book {
char *title;
};
int main() {
struct Book b1;
b1.title = (char *)malloc(100);
strcpy(b1.title, "C Programming");
printf("%s", b1.title);
return 0;
}
Example with arrow operator:
struct Book {
char *title;
};
int main() {
struct Book *b1;
b1 = (struct Book *)malloc(sizeof(struct Book));
b1->title = (char *)malloc(100);
strcpy(b1->title, "Learn C");
printf("%s", b1->title);
return 0;
}
ऊपर दिए गए दोनों programs में हमने structure के अंदर pointer member को access किया है। पहले में object direct है इसलिए dot operator का प्रयोग किया और दूसरे में pointer से structure को access किया इसलिए arrow operator का प्रयोग किया।
Pointer inside Structure के उपयोग
Structure के अंदर pointer का उपयोग कई practical scenarios में किया जाता है, जैसे –
- Dynamic memory allocation के साथ flexible data structure बनाना।
- Linked List, Tree, Graph जैसे complex structures में nodes को आपस में connect करना।
- Large data को memory efficient तरीके से handle करना।
- Runtime में data size के अनुसार memory allocate करना।
Pointer in Structure के लिए Precautions
- Pointer member को use करने से पहले हमेशा memory allocate करें।
- Unused pointers को NULL assign करें ताकि accidental access ना हो।
- Memory leak से बचने के लिए
free()function से allocated memory को release करें।
Pointer vs Normal Member in Structure
| Normal Member | Pointer Member |
|---|---|
| Fixed memory में store होता है | Dynamic memory से linked होता है |
| Direct value store करता है | Memory address store करता है |
| Memory allocation automatic | Manual allocation की आवश्यकता |
| Memory waste हो सकता है | Flexible memory usage |
Final Important Points for Students
- Pointer को समझना जरूरी है क्योंकि C/C++ में यह memory management का foundation है।
- Structure के अंदर pointer का मतलब होता है कि structure dynamic हो गया है।
- इससे आप ऐसे programs बना सकते हैं जो user की जरूरत के अनुसार data handle कर सकें।
- Interview में structure और pointer के combination से जुड़े सवाल बहुत बार पूछे जाते हैं।
FAQs
char *name; या int *ptr;। यह Structure के body के अंदर लिखा जाता है।
malloc() या calloc() का उपयोग किया जाता है। जैसे – ptr = (int *)malloc(sizeof(int));।
. (dot) operator से और अगर pointer है तो -> (arrow) operator से access किया जाता है। जैसे – s1.name या s1->name।