C प्रोग्रामिंग भाषा में Structure एक user-defined डेटा टाइप होता है, जो एक साथ विभिन्न प्रकार के डेटा को एक ही नाम के अंदर स्टोर कर सकता है। सरल भाषा में कहें तो structure एक container की तरह होता है जो related variables को एक साथ रखता है।
कभी-कभी हमें अलग-अलग डेटा जैसे integer, float, character एक साथ रखना होता है। अगर हम इन्हें अलग-अलग variables में रखेंगे तो प्रोग्राम जटिल और बड़ा हो जाएगा। Structure से हम इन सभी को एक ही variable में encapsulate कर सकते हैं।
struct StructureName {
dataType1 member1;
dataType2 member2;
...
};
यहाँ StructureName
आपका structure का नाम है, और इसमें members (variables) होते हैं जो अलग-अलग data types हो सकते हैं।
#include <stdio.h>
struct Student {
int id;
char name[50];
float marks;
};
int main() {
struct Student s1;
s1.id = 101;
strcpy(s1.name, "Rohan");
s1.marks = 85.5;
printf("Student ID: %d\n", s1.id);
printf("Student Name: %s\n", s1.name);
printf("Student Marks: %.2f\n", s1.marks);
return 0;
}
struct Student
नाम का एक structure बनाया जिसमें तीन members हैं: id
, name
, और marks
.s1
नाम का structure variable बनाया गया।.
) से access करके values assign की गईं।strcpy()
का use string copy करने के लिए किया गया है।Structure के अंदर stored data को access करने के लिए dot operator (.
) का use करते हैं।
variableName.memberName;
एक structure से आप कई variables बना सकते हैं:
struct Student s1, s2, s3;
Structure का pointer भी बनाया जा सकता है और -> operator से members access होते हैं।
struct Student *ptr = &s1;
printf("%s", ptr->name);
Structure के अंदर दूसरी structure भी रखी जा सकती है।
struct Date {
int day, month, year;
};
struct Student {
int id;
char name[50];
struct Date dob; // Nested structure
};
Structure C language का एक powerful feature है जो आपको complex data को एक organized तरीके से store करने की सुविधा देता है। इससे programs readable और maintainable बनते हैं। ऊपर दिए गए example से आप structure की बेसिक समझ पा गए होंगे।