struct in C

1๏ธโƒฃ What Is a struct?

A struct (structure) in C is a user-defined data type that allows you to group different types of variables under one name.

๐Ÿ‘‰ Think of it as a box that can hold multiple variables of different types.

๐Ÿ”‘ Why Use struct?

Without struct, if you want to store details of a student (name, age, grade), youโ€™d need separate variables:

char name[50];
int age;
float grade;

With struct, you can group them together:

struct Student {
    char name[50];
    int age;
    float grade;
};

2๏ธโƒฃ Defining a struct

Syntax:

struct StructName {
    dataType member1;
    dataType member2;
    // ... more members
};

๐Ÿ›  Example:

struct Student {
    char name[50];
    int age;
    float grade;
};

3๏ธโƒฃ Declaring a struct Variable

You can declare a variable after defining the struct:

struct Student student1;

Or define and declare together:

struct Student {
    char name[50];
    int age;
    float grade;
} student1, student2;

4๏ธโƒฃ Accessing Members

Use the dot operator (.) to access members:

student1.age = 20;
printf("Age: %d\n", student1.age);

๐Ÿ›  Full Example

#include <stdio.h>

struct Student {
    char name[50];
    int age;
    float grade;
};

int main() {
    struct Student s1;

    // Assign values
    strcpy(s1.name, "Alice");
    s1.age = 20;
    s1.grade = 85.5;

    // Print values
    printf("Name: %s\n", s1.name);
    printf("Age: %d\n", s1.age);
    printf("Grade: %.2f\n", s1.grade);

    return 0;
}

5๏ธโƒฃ Array of Structures

You can create an array of structures to store multiple items.

๐Ÿ›  Example:

struct Student students[3];

strcpy(students[0].name, "Alice");
students[0].age = 20;
students[0].grade = 85.5;

// Similarly for students[1], students[2]

๐Ÿ” Looping:

for (int i = 0; i < 3; i++) {
    printf("Name: %s\n", students[i].name);
    printf("Age: %d\n", students[i].age);
    printf("Grade: %.2f\n", students[i].grade);
}

6๏ธโƒฃ Passing struct to Functions

You can pass:

  • By value (copy)
  • By reference (using pointer)

๐Ÿ›  Example: Passing by Reference

void printStudent(struct Student *s) {
    printf("Name: %s\n", s->name);
    printf("Age: %d\n", s->age);
    printf("Grade: %.2f\n", s->grade);
}

Call:

printStudent(&s1);  // Pass address of s1

Note: Use -> to access members via a pointer.


7๏ธโƒฃ Nested struct

You can have a struct inside another struct.

๐Ÿ›  Example:

struct Date {
    int day, month, year;
};

struct Student {
    char name[50];
    struct Date dob;  // Nested struct
};

Accessing:

student1.dob.day = 15;
printf("DOB: %d/%d/%d\n", student1.dob.day, student1.dob.month, student1.dob.year);

โœ… Common Use Cases of struct

Use CaseWhy struct?
Storing complex dataGroup related data (e.g., Student, Employee, Product details).
Data records (tables)Arrays of structs act like database rows (e.g., a list of students).
Graphics (e.g., Point, Rectangle)Store coordinates, shapes, and properties together.
Networking (e.g., Packet structures)Pack header + payload into one struct.
Linked lists, trees, graphsstruct forms the building block of data structures.

๐Ÿ”ง Tips & Gotchas

  • โœ… Donโ€™t forget to include string.h if you use strcpy() with strings.
  • โœ… Struct variables are copied by value unless you use pointers.
  • โŒ Thereโ€™s no direct support for methods (functions) inside structs (unlike C++).

๐Ÿ”š Conclusion

  • A struct lets you group different types of data under one name.
  • Itโ€™s super useful for organizing complex data.
  • You can create arrays of structs, pass structs to functions, and even nest structs.

๐Ÿ’ก Next challenge: Try writing a program to manage a list of books (title, author, price) using structs.

Feedback Display

Learner Reviews