Structures (struct)
Structures (struct)
What is a Structure?
A structure (struct) is a user-defined data type that groups related variables of different data types under one name. Unlike arrays (which store same-type elements), structures can group int, float, char, etc. together.
Real-world analogy: A student record has: rollNo (int), name (string), marks (float), grade (char). A struct groups all these together.
#include <stdio.h> #include <string.h> // Define the structure struct Student { int rollNo; char name[50]; float marks; char grade; }; int main() { // Declare and initialize struct Student s1; s1.rollNo = 101; strcpy(s1.name, "Rahul Sharma"); s1.marks = 87.5f; s1.grade = 'A'; // Access members with dot operator printf("Roll No : %d\n", s1.rollNo); printf("Name : %s\n", s1.name); printf("Marks : %.1f\n", s1.marks); printf("Grade : %c\n", s1.grade); return 0; }
Initialization Methods
// Method 1: Member-by-member struct Student s1; s1.rollNo = 101; strcpy(s1.name, "Rahul"); s1.marks = 85.0f; // Method 2: At declaration (ordered) struct Student s2 = {102, "Priya", 92.5f, 'A'}; // Method 3: Designated initializers (C99) struct Student s3 = {.rollNo=103, .marks=78.0f, .grade='B'};
Array of Structures
#include <stdio.h> #include <string.h> struct Student { int roll; char name[30]; float marks; }; int main() { struct Student class[3] = { {1, "Rahul", 85.0f}, {2, "Priya", 92.0f}, {3, "Aditya", 78.0f}, }; printf("%-5s %-10s %s\n", "Roll", "Name", "Marks"); printf("----------------------------\n"); for(int i=0; i<3; i++) printf("%-5d %-10s %.1f\n", class[i].roll, class[i].name, class[i].marks); return 0; }
Nested Structures
struct Date { int day, month, year; }; struct Employee { char name[50]; float salary; struct Date dob; // Nested structure }; struct Employee e = {"Gagan", 50000.0f, {15, 8, 1995}}; printf("%s DOB: %d/%d/%d\n", e.name, e.dob.day, e.dob.month, e.dob.year);
Summary
structgroups related variables of different types under one name- Members accessed with dot operator:
var.member - Pointer to struct uses arrow:
ptr->member - Array of structs:
struct Type arr[n]; - Structs can be nested (struct inside struct)
- Use
typedef structto avoid writingstructkeyword every time
Structure क्या है?
Structure (struct) एक user-defined data type है जो अलग-अलग data types के related variables को एक नाम के अंतर्गत group करता है। Arrays के विपरीत (जो same-type elements store करते हैं), structures int, float, char आदि को एक साथ group कर सकते हैं।
#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; }
Members तक पहुँचने के लिए dot operator (.) use करते हैं: s1.rollNo, s1.name। Pointer के लिए arrow operator (->) use करते हैं।
Array of Structures
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);
Programs लिखें: (1) 5 students का struct बनाएं, marks input करें, highest marks वाले का नाम print करें (2) Employee struct बनाएं — name, salary, department के साथ