What is Structure in C in Hindi - C में Structure क्या है
Structure एक User Defined Data type है जिसे Primitive Data Type और Derived Data type की सहायता से बनाया जाता है
Structure आपको विभिन्न प्रकार के Data types वाले Variables को एक Single Name के अंतर्गत Combine करने की अनुमति देते है।
यह आपको अलग अलग Data types वाले Variables को एक Single Unit के अंतर्गत समूह में एक साथ रखने देता है ताकि संबंधित Data को Manage करने में आसानी हो
Structure संबन्धित अन्य महत्पूर्ण बाते
1) Structure विभिन्न प्रकार के Data types वाले एक से ज्यादा Values को विभिन्न allocated memory location में Store करता है
2) Structure की Size उसके सभी Data members की Size के बराबर होती है
3) Structure की Minimum size 1-byte होती है
4) Structure को struct keyword से Declare किया जाता है
5) structure में उसके member variables को declare करने के दौरान उसे Initialize नही कर सकते
6) Member variables को Access करने के लिए (.) Dot Operator का Use करते है
7) Member variables को उसी क्रम में Initialize करना है जिस क्रम मे उसे Declare किए थे।
Declaration of Structure in C in Hindi - C में Structure को Declare करना
C programming में एक Structure को Program में Use करने से पहले उसे Declare किया जाता है
हम एक Structure को Declare करने के लिए निम्न दो Methods का प्रयोग करते है
Method 1:
इस Method में हम structure को main() के अंदर 'struct' keyword का Use करके Declare करते है
Syntax:
struct structure_name {
dataType member_name1;
dataType member_name2;
............
............
};
Example:
struct employee
{
eid int;
char name [30];
float salary;
int phone;
};
int main()
{
struct employee emp;
return 0;
}
ऊपर के कोड मे एक structure `employee` नाम से Define किया गया है जो अपने में four members eid, name,salary और phone को रखता है
`main()` function के अंदर एक `struct employee के प्रकार का एक variable `emp` declare किया गया है
इसका अर्थ है कि आप `employee` structure के instances का निर्माण करते है जिसका Use कर आप विभिन्न employees के बारे मे सूचनाओ को जैसे उनका id, name, salary, और phone को Store कर सकते हैं
Method 2:
इस Method में हम structure को Define करते समय उसे Declare करते है।
Syntax:
struct structure_name {
dataType member_name1;
dataType member_name2;
............
............
} variables;
Example;
struct employee
{
eid int;
char name [30];
float salary;
int phone;
}e1, e2;
ऊपर के Example में employee नाम से Structure, Define किया गया है जिसके 4 Members (eid,name, salary, phone) हैं
इसके अतिरिक्त दो variables e1 और e2, struct employee प्रकार के Declare किए गए है इन variables का Use करके हम विभिन्न employees की ID, name, salary और phone number संबन्धित जानकारी Store कर सकते है
Access Structure Members in C in Hindi - C में Structure member को Access करना
C programming में हम dot (`.`) operator और Pointer का Use करके structure के members को access कर सकते हैं
Dot Operator (`.`) कर Use करके
Dot operator का Use निम्न प्रकार से structure के Members को Access करने के लिए करते हैं
Syntax: structure_variable.member_name
Program for accessing member by Dot operator(.)
#include<stdio.h>
#include<string.h>
// Define the structure
struct employee
{
int eid;
char name[30];
float salary;
};
int main()
{
// Declare a variable of type struct employee
struct employee e1, e2;
//Storing the first employee's Information
e1.eid = 1;
strcpy(e1.name, "Aman Kumar");
e1.salary = 48000.0;
//Storing the second employee's Information
e2.eid = 2;
strcpy(e2.name, "Rahul Kumar");
e2.salary = 56000.0;
// Printing the first employee's Information
printf("First employee's ID : %d\n", e1.eid);
printf("First employee's name: %s\n", e1.name);
printf("First employee's salary: %.2f\n", e1.salary);
// Printing the second employee's Information
printf("Second employee's ID : %d\n", e2.eid);
printf("Second employee's name: %s\n", e2.name);
printf("Second employee's salary: %.2f\n", e2.salary);
return 0;
}
Output:
First employee's ID : 1
First employee's name: Aman Kumar
First employee's salary: 48000.00
Second employee's ID : 2
Second employee's name: Rahul Kumar
Second employee's salary: 56000.00
Explaination:
ऊपर Program मे एक structure `employee` नाम से define किया गया है जिसके members क्रमश: eid, name, and salary है
उसके बाद दो variables `e1` और `e2` declare किया गया है
जिसके द्वारा दो employees की Informations जैसे employee ID, name, और salary को इन variables की मदद से Value Assign किया जाता है
अब `printf` का Use करके दोनों employees(e1 और e2) की `eid`, `name`, और `salary` Print की जाती है
Pointers का Use करके
आप Structures के लिए Pointers का उपयोग करके Structure के Members को भी Access कर सकते है इसके लिए Dot operator के बदले arrow operator (`->`) का Use pointers के साथ करके आप structure members को Access कर सकते हैं।
Program for accessing member by Pointer
#include<stdio.h>
#include<string.h>
// Define the structure
struct employee
{
int id;
char name[30];
float salary;
};
int main()
{
struct employee e1, e2;
struct employee *ptrE1 = &e1;
struct employee *ptrE2 = &e2;
// Storing information for the first employee
ptrE1->id = 1;
strcpy(ptrE1->name, "Chetan Kumar");
ptrE1->salary = 65000.0;
// Storing information for the second employee
ptrE2->id = 2;
strcpy(ptrE2->name, "Rahul Kumar");
ptrE2->salary = 70000.0;
// Printing information for the first employee
printf("First Employee's ID: %d\n", ptrE1->id);
printf("First Employee's Name: %s\n", ptrE1->name);
printf("First Employee's Salary: %.2f\n\n", ptrE1->salary);
// Printing information for the second employee
printf("Second Employee's ID: %d\n", ptrE2->id);
printf("Second Employee's Name: %s\n", ptrE2->name);
printf("Second Employee's Salary: %.2f\n", ptrE2->salary);
return 0;
}
Output:
First Employee's ID: 1
First Employee's Name: Chetan Kumar
First Employee's Salary: 65000.00
Second Employee's ID: 2
Second Employee's Name: Rahul Kumar
Second Employee's Salary: 70000.00
ऊपर Program में हमने Dot operator के बदले Pointer का Use किया है
Array of Structure in C in Hindi
C में Structure का एक array आपको एक Structure की कई Copies बनाने और उसे एक array के row के अंदर रखने की अनुमति देता है
यह उस समय मददगार साबित होता है जब आप एक ही प्रकार के Data elements के Collection के साथ कार्य कर रहे हैं और प्रत्येक element structure द्वारा represent किया जाता है
Program for Array of Structure in C
#include<stdio.h>
#include<string.h>
struct employee
{
int id;
char name[30];
float salary;
};
int main()
{
struct employee emp[4];
emp[0].id =101;
strcpy(emp[0].name, "Dolly singh");
emp[0].salary = 45000.0;
// Initialize the second employee
emp[1].id = 102;
strcpy(emp[1].name, "Amita netam");
emp[1].salary = 30000.0;
// Initialize the third employee
emp[2].id = 104;
strcpy(emp[2].name, "Ketika sahu");
emp[2].salary = 50000.0;
// Initialize the fourth employee
emp[3].id = 104;
strcpy(emp[3].name, "Soniya yadav");
emp[3].salary = 36000.0;
// Printing information for all employees
for (int i = 0; i < 4; i++)
{
printf("Employee ID: %d\n", emp[i].id);
printf("Employee Name: %s\n", emp[i].name);
printf("Employee Salary: %.2f\n\n", emp[i].salary);
}
return 0;
}
Output:
Employee ID: 101
Employee Name: Dolly singh
Employee Salary: 45000.00
Employee ID: 102
Employee Name: Amita netam
Employee Salary: 30000.00
Employee ID: 104
Employee Name: Ketika sahu
Employee Salary: 50000.00
Employee ID: 104
Employee Name: Soniya yadav
Employee Salary: 36000.00
Explanation:
ऊपर के Program में Structures के Array को दिखाया गया है
1) इसमें सबसे पहले employee नामक एक Structure का निर्माण किया गया है ताकि उसमे four employees की ID, name और salary संबन्धित Information को Store किया जा सके
2) struct employee type कर `emp' नामक एक Array जिसका size, 4 हैं, Declare किया जाता है ताकि उसमे four employees का Information Store किया जा सके
3) उसके बाद Program प्रत्येक employee के Data को Array Index का Use करके Initialize करता है और एक loop का Use करके सभी emoloyees की Informations को Print किया जाता है
Difference between array and structure in c in hindi
1) एक Array एक ही प्रकार के Data type वाले elements का Collection है जो contiguous(मिला हुआ) memory locations में Store होते है
एक Structure विभिन्न प्रकार के Data type वाले elements का एक Collection है जो एक Single name के अंतर्गत एक साथ समुह में होते है
2) एक Array के सभी elements को समान Data types का होना आवश्यक है
Structure के elements विभिन्न Data types वाले हो सकते हैं
3) Array में समान प्रकार के elements के लिए fixed number में Memory allocate किया जाता है
Structure में प्रत्येक member के लिए स्वतंत्र रूप से Memory allocate किया जाता है
4) एक Array में elements को Index द्वारा Access किया जाता है जहां पर Index element की position को बताता है
एक Structure, में elements को dot operator का Use करके Access किया जाता है
5) Array का Use सामान्यत: समान Data elements को Store और manipulate करने के लिए किया जाता है
Structure का Use विभिन्न Data types वाले सम्बंधित Data elements के समूह को एक Single Unit में Group करने के लिए किया जाता है
6) Array एक ही प्रकार के बडे़ number के element को memory में store करने के लिए ज्यादा दक्ष है
Structures members के alignment के लिए उनके बीच padding के लिए ज्यादा memory Use कर सकता है
7) Array के पास declare करते समय fixed size होती है और size को runtime के दौरान बदल नही सकते
Structure, members को Add करने, Remove करने या Modify करने मे flexible होता है जिससे Data को dynamic रूप से संगठित करने की अनुमति मिलती है
Difference between Union and Structure in c in hindi
1) Structure प्रत्येक member के लिए स्वतंत्र रूप से Memory Allocate करता है जिससे प्रत्येक member को अलग से memory location मिलता है
Union में memory को इस प्रकार से Allocate किया जाता है कि सारे members एक ही memory location को share करते है
2) एक Structure की size उसके सारे members की size का sum रहता है जिसमे alignment के लिए Padding भी शामिल है
एक Union की size इसके सबसे बडे़ member की size होती है जिसमे कोई अतिरिक्त padding शामिल नहीं होता है
3) एक Structure में, प्रत्येक member के स्वयं की Unique memory address होते है जिसे आप स्वतंत्र रूप से प्रत्येक member के लिए Access कर सकते हैं
एक Union, में सभी members एक ही memory address को share करते है और एक member को access करने पर अन्य members की Values प्रभावित होती है
4) जब एक Structure को Initialize किया जाता है तब हम उसके प्रत्येक member को स्वतंत्र रूप से शुरुवाती value देकर Initialize कर सकते हैं
जब एक Union को Initialize करते है तब उसके केवल एक Member को एक समय में ही Initialize कर सकते हैं
एक member के value को बदलने से अन्य प्रभावित होते है
5) Structure इस प्रकार से Data Store करता है कि सभी members को उसे एक साथ Access करने की अनुमति मिल जाती है
Union इस प्रकार से Data Store करता है कि एक समय में केवल एक members के Data को Access किया जा सकता है को
6) Structure का सामान्यत: Use जटिल Data types का निर्माण करने और संबन्धित Data को संगठित करने के लिए किया जाता है
Union का Use उस समय पर किया जाता है जब एक सीमित Memory space के अन्दर विभिन्न प्रकार के Data types को Handle करना होता है
7) Structure में एक member को Update किया जाए तो अन्य members प्रभावित नही होते है
Union में एक member को Update किया जाए तो अन्य members प्रभावित होते है क्योंकि ये एक ही memory location को Share करते है
Related Post
0 टिप्पणियाँ