🟡 Intermediate · Lesson 27
Structures (struct)
Structure क्या है?
Structure (struct) एक user-defined data type है जो अलग-अलग data types के related variables को एक नाम के अंतर्गत group करता है। Arrays के विपरीत (जो same-type elements store करते हैं), structures int, float, char आदि को एक साथ group कर सकते हैं।
C Language – Basic Struct
#include <stdio.h> #include <string.h> struct Student { int rollNo; char name[50]; float marks; char grade; }; int main() { struct Student s1 = {101, "Rahul Sharma", 87.5f, 'A'}; printf("Roll No : %d\n", s1.rollNo); printf("Naam : %s\n", s1.name); printf("Marks : %.1f\n", s1.marks); printf("Grade : %c\n", s1.grade); return 0; }
Roll No : 101
Naam : Rahul Sharma
Marks : 87.5
Grade : A
💡 Key Point
Members तक पहुँचने के लिए dot operator (.) use करते हैं: s1.rollNo, s1.name। Pointer के लिए arrow operator (->) use करते हैं।
Array of Structures
C Language
struct Student class[3] = { {1, "Rahul", 85.0f}, {2, "Priya", 92.0f}, {3, "Aditya", 78.0f}, }; for(int i=0; i<3; i++) printf("%d %s %.1f\n", class[i].roll, class[i].name, class[i].marks);
🏋️ Practice
Programs लिखें: (1) 5 students का struct बनाएं, marks input करें, highest marks वाले का नाम print करें (2) Employee struct बनाएं — name, salary, department के साथ
अक्सर पूछे जाने वाले प्रश्न (FAQ)
C में structure क्या है?
Structure एक user-defined type है जो कई संबंधित variables — संभवतः अलग types के — को एक नाम के अंतर्गत समूहित करता है। जैसे एक
Student structure name, roll number और marks साथ रख सकता है, आपको एक student को एक इकाई की तरह मानने देते हुए।C में structure के members कैसे access करते हैं?
Structure variable के साथ आप dot operator इस्तेमाल करते हैं, जैसे
s.name। अगर structure का pointer है, तो arrow operator इस्तेमाल करते हैं, जैसे p->name। दोनों member तक पहुँचते हैं; कौन-सा आप इस्तेमाल करें यह इस पर निर्भर कि आपके पास value है या pointer।Array of structures क्या है?
Array of structures कई structure values को सूची में रखता है, जैसे पचास students रखने को
struct Student class[50];। फिर आप हर एक को index से और उसके members को dot operator से access करते हैं, जैसे class[i].marks, जो record सूचियों के लिए आदर्श है।C में nested structure क्या है?
Nested structure वह structure है जो किसी दूसरे structure को member के रूप में रखता है। जैसे एक
Student structure जन्म तिथि के लिए एक Date structure रख सकता है। आप भीतरी members तक dot operator को जोड़कर पहुँचते हैं, जैसे s.dob.year।structure और array में क्या अंतर है?
Array एक ही type के कई items index से access करके रखता है, जबकि structure अलग types के items को नाम से access करके समूहित करता है। आप अक्सर इन्हें मिलाते हैं — array of structures — कई records रखने को जिनमें से हर एक के कई fields हों।
💻 लाइव कोड एडिटर
इस पेज के प्रोग्राम यहीं तैयार हैं — चलाएँ, बदलें और सीखें। कुछ भी इंस्टॉल किए बिना।
OneCompiler द्वारा संचालित। कोड एडिटर में अपने आप आ जाता है — Run दबाकर आउटपुट देखें। अगर एडिटर न खुले तो नए टैब में खोलें.