Defining Structure in Hindi
/ BCA / Programming with C and CPP
Structure in C Explained with Syntax and Keyword Usage
Defining Structure in Hindi
Structure क्या होता है?
C भाषा में Structure एक ऐसा User Defined Data Type (UDT) होता है जिसकी सहायता से हम एक से अधिक Data Types को एक साथ जोड़कर एक Complex Data Type बना सकते हैं। Structure की मदद से हम एक ही नाम के अंतर्गत अलग-अलग प्रकार की जानकारी को स्टोर कर सकते हैं। जैसे मान लीजिए हमें एक Student की जानकारी रखनी है जिसमें उसका नाम (string), उम्र (int), और उसके marks (float) शामिल हों, तो हम Structure का उपयोग करके ये सारी जानकारी एक ही जगह रख सकते हैं।
Structure की आवश्यकता क्यों पड़ी?
- अगर हमें किसी व्यक्ति (जैसे Student, Employee) की Details को Store करना हो जिसमें अलग-अलग प्रकार के Data हों (int, float, char आदि), तो एक Simple Variable से ये संभव नहीं है।
- Array सिर्फ एक ही प्रकार के Data को संभाल सकता है, लेकिन Structure के अंदर हम अलग-अलग प्रकार के Data को एक साथ Store कर सकते हैं।
- Structure प्रोग्रामिंग को Modular बनाता है और Real World Entities को Represent करने में मदद करता है।
What is a Structure in C in Hindi
Structure in C की परिभाषा (Definition)
C भाषा में Structure एक Collection होता है जिसमें हम विभिन्न प्रकार के Variables को एक नाम के अंतर्गत रख सकते हैं। इसे हम struct Keyword की मदद से Define करते हैं। Structure का उपयोग करके हम Complex Data Structures जैसे Linked List, Tree, Graph आदि भी बना सकते हैं।
Structure के मुख्य Parts
- struct Keyword: Structure को Define करने के लिए struct Keyword का उपयोग किया जाता है।
- Structure Name: यह उस Structure का नाम होता है जिससे हम Structure को पहचानते हैं।
- Members: Structure के अंदर विभिन्न प्रकार के Variables (Members) होते हैं जिन्हें हम Structure के अंदर Define करते हैं।
Structure Example की जरूरत क्यों है?
मान लीजिए हमें एक Book की जानकारी Store करनी है जिसमें Book का नाम, लेखक का नाम और उसकी कीमत हो। अगर हम इन तीनों को अलग-अलग Variables में Store करें तो यह प्रोग्रामिंग को Complex बना देगा। लेकिन Structure की मदद से हम इन सभी जानकारी को एक Logical Unit में रख सकते हैं:
struct Book {
char name[50];
char author[50];
float price;
};
Syntax to Define Structure in Hindi
Structure को Define करने का Syntax
struct StructureName {
data_type member1;
data_type member2;
...
data_type memberN;
};
आइए एक उदाहरण से समझते हैं:
struct Student {
char name[50];
int age;
float marks;
};
Structure Variable को Declare करना
Structure को Define करने के बाद, हमें उसके Object या Variable को Declare करना होता है:
struct Student s1;
यहां s1 एक Structure Variable है जो Student Structure का एक Object है। इसके अंदर हम सभी Members को Access कर सकते हैं।
Structure Members को Access कैसे करें?
Structure के अंदर मौजूद Members को Access करने के लिए dot (.) Operator का उपयोग किया जाता है।
s1.age = 20;
s1.marks = 88.5;
strcpy(s1.name, "Ravi");
Structure में Input और Output कैसे लें?
#include <stdio.h>
#include <string.h>
struct Student {
char name[50];
int age;
float marks;
};
int main() {
struct Student s1;
printf("Enter name: ");
gets(s1.name);
printf("Enter age: ");
scanf("%d", &s1.age);
printf("Enter marks: ");
scanf("%f", &s1.marks);
printf("\nStudent Details:\n");
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Marks: %.2f\n", s1.marks);
return 0;
}
Use of struct Keyword in Hindi
struct Keyword का उपयोग
- Structure को Define करने के लिए struct Keyword जरूरी होता है।
- struct की मदद से हम Structure को Create करते हैं, जो अलग-अलग Data Types को एक साथ Hold करता है।
struct Keyword के उपयोग का उदाहरण
struct Employee {
int id;
char name[50];
float salary;
};
यहां struct Employee एक Structure Define कर रहा है जिसमें एक कर्मचारी की ID, नाम और वेतन की जानकारी रखी गई है।
Structure को एक साथ Define और Declare करना
struct Employee {
int id;
char name[50];
float salary;
} e1, e2;
इस प्रकार हम एक ही समय पर Structure Define और Variables Declare कर सकते हैं।
Structure में Array का उपयोग
Structure के अंदर हम Array का उपयोग करके एक से अधिक Data Store कर सकते हैं:
struct Student {
char name[50];
int age;
float marks[5];
};
Structure का उपयोग Function के साथ
Structure को हम Function के Parameter के रूप में भी Pass कर सकते हैं:
void printStudent(struct Student s) {
printf("Name: %s\n", s.name);
printf("Age: %d\n", s.age);
}
int main() {
struct Student s1 = {"Aman", 18};
printStudent(s1);
return 0;
}
Structure का Memory Allocation
Structure में जितने Members होते हैं, उतनी Memory Allocate होती है:
| Data Type | Memory (Bytes) |
|---|---|
| int | 4 |
| float | 4 |
| char | 1 |
अगर हमारे Structure में int, float और char है, तो कुल Memory 9 Bytes तक जा सकती है (Padding के कारण यह थोड़ा अधिक भी हो सकता है)।
Structure vs Array
| Structure | Array |
|---|---|
| अलग-अलग प्रकार के Data Store करता है | सिर्फ एक ही प्रकार के Data Store करता है |
| Real World Entities को Represent करता है | सिर्फ Simple Data को Manage करता है |
| Accessing dot (.) से होता है | Index द्वारा Access किया जाता है |
Structure के लाभ
- Real World Objects को Represent करना आसान होता है।
- Modular Code बनता है।
- Code को Maintain करना सरल होता है।
- Complex Data Structures बनाना आसान होता है।
FAQs
struct Student { char name[50]; int age; float marks; };
struct Student s1;
s1.age = 18;
s1.marks = 90.5;