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