Structure Assignment in Hindi
/ BCA / Programming with C and CPP
Structure Assignment in C Explained with Examples
Structure Assignment in Hindi
Introduction to Structure Assignment
C language में Structure एक ऐसा user-defined data type होता है जिसमें हम अलग-अलग type के variables को एक group में रख सकते हैं। जैसे - एक Student की जानकारी रखने के लिए हम उसका नाम, उम्र, रोल नंबर, आदि एक ही Structure में रख सकते हैं।
जब हम एक Structure बना लेते हैं, तो अगला जरूरी स्टेप होता है – Structure assignment या कहें – एक Structure variable को दूसरे Structure variable के बराबर assign करना। यह बहुत beginner-friendly concept है लेकिन इसे ठीक से समझना ज़रूरी होता है।
Assigning One Structure to Another in Hindi
C language में अगर दो variables एक ही structure type के हों, तो हम एक variable के सारे data दूसरे को simply "=" operator से दे सकते हैं। इसे Structure Assignment कहते हैं।
Structure Assignment Syntax
#include <stdio.h>
struct Student {
int roll;
char name[50];
};
int main() {
struct Student s1 = {101, "Amit"};
struct Student s2;
s2 = s1; // Structure Assignment
printf("Roll: %d, Name: %s", s2.roll, s2.name);
return 0;
}
ऊपर दिए गए उदाहरण में s1 और s2 एक ही structure type के हैं। जब हम s2 = s1; करते हैं, तब s1 के सभी data members की values s2 को assign हो जाती हैं। यानी अब s2 में वही जानकारी है जो s1 में थी।
Important Points about Structure Assignment
- Structure assignment तभी possible होता है जब दोनों variables same structure type के हों।
- Assignment में सभी data members की values copy होती हैं।
- अगर Structure में कोई array या nested structure हो, तब भी direct assignment होता है।
- लेकिन अगर Structure में pointers हों, तब सिर्फ address copy होते हैं, data नहीं।
Copying Structure Data in Hindi
Structure data को copy करने के दो तरीके हो सकते हैं:
- Direct assignment से – जैसा ऊपर किया गया।
- Manually हर member को copy करना।
Manual Copying Example
#include <stdio.h>
#include <string.h>
struct Student {
int roll;
char name[50];
};
int main() {
struct Student s1 = {102, "Ravi"};
struct Student s2;
s2.roll = s1.roll;
strcpy(s2.name, s1.name);
printf("Roll: %d, Name: %s", s2.roll, s2.name);
return 0;
}
यह तरीका useful तब होता है जब आप केवल कुछ members को copy करना चाहते हैं, ना कि पूरा structure। लेकिन अगर पूरा structure copy करना हो तो direct assignment ज्यादा efficient होता है।
When Not to Use Structure Assignment
- अगर Structure में file pointers, dynamic memory pointers या hardware-related addresses हों।
- अगर आप partial data copy करना चाहते हों।
- अगर आपको deep copy की जरूरत हो (नीचे समझाया गया है)।
Shallow Copy vs Deep Copy in Structures in Hindi
What is Shallow Copy?
Shallow Copy का मतलब है – सिर्फ values को copy करना, वो भी सिर्फ surface level पर। अगर Structure में pointer members हों, तो shallow copy में केवल pointer address copy होता है, data नहीं। यानी दो variables एक ही memory location को point करते हैं।
Example of Shallow Copy
#include <stdio.h>
struct Data {
int *ptr;
};
int main() {
int a = 10;
struct Data d1, d2;
d1.ptr = &a;
d2 = d1; // shallow copy
printf("%d", *(d2.ptr)); // 10
return 0;
}
इसमें d2.ptr और d1.ptr दोनों एक ही memory को point कर रहे हैं। अगर हम d1 या d2 से value को modify करते हैं तो दूसरे में भी फर्क पड़ता है।
What is Deep Copy?
Deep Copy का मतलब है – original data को new memory में copy करना। इसका मतलब यह होता है कि pointer के द्वारा जो data point किया जा रहा है, उसकी भी एक copy बनानी पड़ेगी। इससे दो अलग-अलग memory locations बनती हैं।
Example of Deep Copy
#include <stdio.h>
#include <stdlib.h>
struct Data {
int *ptr;
};
int main() {
struct Data d1, d2;
d1.ptr = (int *)malloc(sizeof(int));
*d1.ptr = 20;
d2.ptr = (int *)malloc(sizeof(int));
*d2.ptr = *d1.ptr; // deep copy
*d2.ptr = 40;
printf("d1: %d\n", *d1.ptr); // 20
printf("d2: %d\n", *d2.ptr); // 40
free(d1.ptr);
free(d2.ptr);
return 0;
}
ऊपर deep copy में d1 और d2 दोनों के पास अलग-अलग memory है। इसलिए जब हम d2 की value को 40 करते हैं तो d1 की value 20 ही रहती है। इसका फायदा यह है कि एक variable की change दूसरे को affect नहीं करती।
Table: Difference Between Shallow Copy and Deep Copy
| Feature | Shallow Copy | Deep Copy |
|---|---|---|
| Pointer Data | Pointer address ही copy होता है | Pointer data की नई memory बनाई जाती है |
| Memory Allocation | New memory allocate नहीं होती | New memory allocate होती है |
| Data Independence | Changes दूसरे variable को affect करते हैं | Changes दूसरे variable को affect नहीं करते |
| Usage | Simple assignments | Critical applications जैसे file handling |
Best Practices
- अगर structure में सिर्फ simple data types हों (int, float, char etc.) तो structure assignment safe है।
- अगर structure में pointers हों, तो ध्यान से assignment करें।
- जब भी pointer structure को assign करें, memory leakage से बचने के लिए free() का use करें।
Why Structure Assignment is Useful?
- Code को छोटा और readable बनाता है।
- Multiple values को एक साथ transfer करना आसान होता है।
- Program की efficiency बढ़ती है, खासकर जब same type की values को कई जगह use करना हो।