Array of Structure in Hindi
/ BCA / Programming with C and CPP
Array of Structure in C Programming Explained in Hindi
Array of Structure in Hindi
What is Array of Structure in Hindi
जब हम programming में कई सारे एक जैसे data को store करना चाहते हैं, तो हम array का उपयोग करते हैं। वहीं, जब हमें अलग-अलग प्रकार के data (जैसे नाम, उम्र, रोल नंबर आदि) को एक साथ रखना होता है, तो हम structure का उपयोग करते हैं। अब सोचिए, अगर हमारे पास कई सारे students की जानकारी है और हर student का नाम, उम्र, रोल नंबर अलग है, तो हम हर एक student की जानकारी को एक structure में रख सकते हैं, और उन सभी structures को एक array में रख सकते हैं। यही होता है - Array of Structure।
Array of Structure का उपयोग तब किया जाता है जब हमें एक ही type के कई structure को एक साथ manage करना होता है, जैसे कि 10 students की जानकारी, 20 employees का record, आदि।
Defining Array of Structures in Hindi
Structure को define करने के लिए हम struct keyword का उपयोग करते हैं। जब हम structure बना लेते हैं, तो हम उसका एक array बना सकते हैं। नीचे इसका syntax और example दिया गया है:
// एक structure बनाएं जिसमें student की details हों
struct Student {
char name[50];
int roll;
int age;
};
// अब इस structure का array बनाएं
struct Student students[5]; // 5 students के लिए array
ऊपर के कोड में हमने एक structure Student बनाया जिसमें 3 fields हैं: name, roll और age। फिर हमने उसी structure का एक array students नाम से बनाया जिसमें 5 students की details store की जा सकती हैं।
Accessing Elements in Structure Array in Hindi
अब जब हमारे पास structure का array है, तो हम उसमें मौजूद individual elements को कैसे access करें? इसका तरीका बिलकुल आसान है। हम पहले array के index से structure तक पहुँचते हैं, फिर dot (.) operator से field तक।
#include <stdio.h>
struct Student {
char name[50];
int roll;
int age;
};
int main() {
struct Student students[3] = {
{"Amit", 1, 18},
{"Priya", 2, 19},
{"Raj", 3, 17}
};
printf("First Student Name: %s\n", students[0].name);
printf("Second Student Roll Number: %d\n", students[1].roll);
printf("Third Student Age: %d\n", students[2].age);
return 0;
}
ऊपर के program में हमने 3 students का data initialize किया है और फिर अलग-अलग index से data access किया है। students[0].name का मतलब है array का पहला element और उसका name field।
Using Loops with Structure Arrays in Hindi
अगर हमें structure array में मौजूद सभी elements को एक साथ process करना हो, जैसे सभी students की जानकारी print करना या input लेना हो, तो हम loop का उपयोग कर सकते हैं। for loop structure array के साथ बहुत उपयोगी होता है।
आइए एक example देखें जहाँ हम user से 3 students की details input लेते हैं और फिर उसे print करते हैं:
#include <stdio.h>
struct Student {
char name[50];
int roll;
int age;
};
int main() {
struct Student students[3];
int i;
// Input लेना
for(i = 0; i < 3; i++) {
printf("Enter name of student %d: ", i+1);
scanf("%s", students[i].name);
printf("Enter roll number: ");
scanf("%d", &students[i].roll);
printf("Enter age: ");
scanf("%d", &students[i].age);
}
// Output दिखाना
printf("\n--- Student Details ---\n");
for(i = 0; i < 3; i++) {
printf("Student %d:\n", i+1);
printf("Name: %s\n", students[i].name);
printf("Roll: %d\n", students[i].roll);
printf("Age: %d\n", students[i].age);
}
return 0;
}
इस program में हमने 3 students के लिए loop चलाया और user से name, roll, और age input लिया। फिर उसी तरह loop में सारी जानकारी print भी की। इससे आप समझ सकते हैं कि loops के साथ structure array का इस्तेमाल कैसे होता है।
Array of Structures कब Use करें?
- जब एक जैसे multiple records को handle करना हो जैसे कि कई सारे employees, students, books आदि।
- जब हर record में multiple information हो (जैसे name, id, salary)।
- जब data को एक structured और organized तरीके से रखना हो।
Important Points
- Structure के अंदर हम अलग-अलग type का data रख सकते हैं।
- Structure array एक ही type के कई structure को manage करता है।
- Dot operator (.) का इस्तेमाल करके हम structure के अंदर के fields को access करते हैं।
- Loop का इस्तेमाल करके हम आसानी से सारे records को input और output कर सकते हैं।
Structure Array में Sorting कैसे करें (Example)
मान लीजिए हमें students को उनके roll number के अनुसार sort करना है:
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int roll;
int age;
};
int main() {
struct Student students[3] = {
{"Priya", 3, 18},
{"Amit", 1, 17},
{"Raj", 2, 19}
};
struct Student temp;
int i, j;
// Sorting using bubble sort
for(i = 0; i < 3; i++) {
for(j = i+1; j < 3; j++) {
if(students[i].roll > students[j].roll) {
temp = students[i];
students[i] = students[j];
students[j] = temp;
}
}
}
printf("Sorted Students by Roll Number:\n");
for(i = 0; i < 3; i++) {
printf("Name: %s, Roll: %d, Age: %d\n", students[i].name, students[i].roll, students[i].age);
}
return 0;
}
इस program में हमने structure array को roll number के अनुसार sort किया है। इससे पता चलता है कि structure array के साथ हम sorting और अन्य operations भी आसानी से कर सकते हैं।
Structure Array के Real Life Uses
- Student Management System में सभी students का data रखने के लिए
- Employee Record Management में
- Library Management System में books का data store करने के लिए
- Customer Records के लिए banking applications में
Final Tips for Beginners
- पहले simple structure बनाना सीखें, फिर array के साथ practice करें।
- Code को धीरे-धीरे समझें और हर step को logically सोचें।
- Structure और array दोनों C की important concepts हैं, अच्छे से practice करें।
- Real life examples से logic को जोड़ें जैसे school, employees, products आदि।
FAQs
struct keyword से, और फिर उस structure का array बनाया जाता है। जैसे:struct Student { char name[50]; int roll; int age; };struct Student students[5]; – यह 5 students का structure array define करता है।
students[0].name पहले student का नाम access करेगा।
for loop का use करते हैं। इससे हम input ले सकते हैं या सभी data को एक साथ print कर सकते हैं। Example:for(i = 0; i < n; i++) { printf("%s", students[i].name); }