Structures in C Language: Full Guide with Examples, Nested Structures, Memory, and More

 

🔧 Structures in C

Defining and Processing Structures with Complete Examples

🔹 What is a Structure in C?

In C, a structure is a user-defined data type that allows you to group variables of different types under one name. It's especially useful when you want to represent real-world entities like a Student, Employee, or Book with multiple attributes.

🔹 Defining a Structure

✅ Syntax

struct structure_name {
    data_type member1;
    data_type member2;
    ...
};

✅ Example 1: Structure Definition and Declaration

#include <stdio.h>

struct Student {
    int id;
    char name[50];
    float marks;
};

int main() {
    struct Student s1;

    s1.id = 1;
    strcpy(s1.name, "Jatasya");
    s1.marks = 88.5;

    printf("ID: %d\n", s1.id);
    printf("Name: %s\n", s1.name);
    printf("Marks: %.2f\n", s1.marks);

    return 0;
}

🟢 Output:

ID: 1
Name: Jatasya
Marks: 88.50

🔹 Initializing Structure at Declaration

struct Student s2 = {2, "Vivan", 76.0};

🔹 Accessing Members with Dot Operator

printf("%d", s2.id);
printf("%s", s2.name);

🔹 Using Pointers with Structures

✅ Example 2: Accessing with Pointer

#include <stdio.h>

struct Book {
    int book_id;
    char title[50];
};

int main() {
    struct Book b = {101, "C Programming"};
    struct Book *ptr = &b;

    printf("ID: %d\n", ptr->book_id);
    printf("Title: %s\n", ptr->title);

    return 0;
}

🟢 Output:

ID: 101
Title: C Programming

🔹 Accepting User Input in Structure

✅ Example 3: Input from User

#include <stdio.h>

struct Employee {
    int id;
    char name[30];
};

int main() {
    struct Employee e;

    printf("Enter ID: ");
    scanf("%d", &e.id);

    printf("Enter Name: ");
    scanf("%s", e.name);

    printf("Employee ID: %d, Name: %s\n", e.id, e.name);
    return 0;
}

🟢 Output:

Enter ID: 12
Enter Name: Jiyansh
Employee ID: 12, Name: Jiyansh

🔹 Array of Structures

✅ Example 4: Student Records

#include <stdio.h>

struct Student {
    int id;
    char name[30];
};

int main() {
    struct Student s[2];

    for (int i = 0; i < 2; i++) {
        printf("Enter ID and Name: ");
        scanf("%d %s", &s[i].id, s[i].name);
    }

    printf("Student Records:\n");
    for (int i = 0; i < 2; i++) {
        printf("ID: %d, Name: %s\n", s[i].id, s[i].name);
    }

    return 0;
}

🔹 Passing Structure to Function

✅ Example 5: Pass by Value

#include <stdio.h>

struct Student {
    int id;
};

void display(struct Student s) {
    printf("Student ID: %d\n", s.id);
}

int main() {
    struct Student s1 = {10};
    display(s1);
    return 0;
}

✅ Example 6: Pass by Reference

void update(struct Student *s) {
    s->id = 999;
}

🔹 Nested Structures

✅ Example 7: Structure Inside Structure

#include <stdio.h>

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

struct Student {
    int id;
    struct Date dob;
};

int main() {
    struct Student s = {1, {15, 8, 2000}};

    printf("ID: %d, DOB: %02d-%02d-%d\n", s.id, s.dob.day, s.dob.month, s.dob.year);
    return 0;
}

🟢 Output:

ID: 1, DOB: 15-08-2000

🔹 Using typedef with Structures

typedef struct {
    int x;
    int y;
} Point;

Point p1 = {3, 4};

🔹 Structure Memory and Padding

Use sizeof() to see memory used by structure. Padding may add extra bytes for alignment.

printf("Size of struct: %lu", sizeof(struct Student));

✅ Conclusion

Structures in C are powerful tools that let you manage multiple variables of different types under one name. They're essential for real-world applications like employee records, student databases, and more. With dot (.), arrow (->), and pointer tricks, you can process and pass structures easily and efficiently.

❓ Frequently Asked Questions

1. Can we use functions inside structures?

Not in C (only possible in C++ using classes).

2. What's the difference between array and structure?

Array holds multiple elements of same type, structure holds multiple types.

3. How are structures passed to functions?

Either by value (copy) or by reference (pointer).

4. What is the size of an empty structure?

It's compiler dependent, often 1 byte.

5. Can structures be recursive?

Yes, by using a pointer to the same structure inside it.


Post a Comment

0 Comments