Structure And Union in C Language Part 2

Mastering C Programming: A Comprehensive Guide to Structures, Pointers, and More



Nesting of Structures
In C programming, structures can be nested within each other, allowing for the creation of more complex data structures. Let's explore how to nest a structure within another structure through the following example:
    #include <stdio.h>

    struct xy {
        int x, y;
    };

    struct xyz {
        struct xy b;
        int z;
    };

    int main() {
        struct xyz a;
        a.b.x = 5, a.b.y = 6, a.z = 8;

        printf("\nThe point is: %d %d %d", a.b.x, a.b.y, a.z);

        return 0;
    }
Here, two structures, xy and xyz, are nested.

Pointers and Structures
Passing structures to functions can be done using call by value or call by reference. Utilizing pointers to structures is one way to implement call by reference. Let's see an example:

    #include <stdio.h>

    struct Emp {
        char name[10];
        int age;
    } e, *eptr;

    int main() {
        eptr = &e;  // Initialize the pointer

        // Accessing structure elements using the arrow operator ->
        eptr->age = 30;

        printf("Employee's age is %d\n", eptr->age);

        return 0;
    }
Structure and Function
C supports passing structure values as arguments to functions. When passing a structure as a function argument, consider whether to pass it by value or by reference. Here's an example demonstrating arithmetic operations on structure members:

    #include <stdio.h>

    struct nos {
        int a, b;
        int add, sub, mul;
        float div;
    };

    struct nos cal(struct nos n);

    int main() {
        struct nos n;

        // Input two numbers
        printf("Enter 2 nos: ");
        scanf("%d%d", &n.a, &n.b);

        n = cal(n);

        // Output the results
        printf("sum=%d\nsub=%d\nmul=%d\ndiv=%.2f", n.add, n.sub, n.mul, n.div);

        return 0;
    }

    struct nos cal(struct nos n) {
        n.add = n.a + n.b;
        n.sub = n.a - n.b;
        n.mul = n.a * n.b;
        n.div = (float)n.a / n.b;

        return n;
    }
Typedef Declaration
C allows the use of typedef to define an identifier for an existing data type. This improves code readability. Here's an example:

    #include <stdio.h>

    typedef int age;

    int main() {
        age mom, dad, son;

        printf("Enter ages\n");
        printf("mom: ");
        scanf("%d", &mom);
        printf("dad: ");
        scanf("%d", &dad);
        printf("son: ");
        scanf("%d", &son);

        printf("Ages of mom, dad, son\n");
        printf("%d\t%d\t%d", mom, dad, son);

        return 0;
    }
Union
Unions in C are derived data types, grouping different variables together. Unlike structures, all members of a union share the same memory location. Here's an example:
  
    #include <stdio.h>

    union Emp {
        char Ecode[10];
        float Esal;
    } e1;

    int main() {
        // Get the size of union Emp e1
        int size = sizeof(e1);
        printf("Size of Emp e1: %d\n", size);

        // Input employee code
        printf("Enter Ecode: ");
        scanf("%s", e1.Ecode);
        printf("Ecode: %s\n", e1.Ecode);

        // Input employee salary
        printf("Enter Esal: ");
        scanf("%f", &e1.Esal);
        printf("Esal: %.2f\n", e1.Esal);

        return 0;
    }
Enum Data Type
Enumerated data types in C are user-defined data types declared using the enum keyword. Here's an example:
  
    #include <stdio.h>

    enum color { red, blue, pink, gray, yellow };

    int main() {
        enum color c1, c2;

        c1 = red;
        c2 = gray;

        if (c1 == c2)
            printf("Both Are Equal");
        else
            printf("Both Are Different");

        return 0;
    }
  
Conclusion:
In this comprehensive guide, we've delved into the intricacies of C programming, exploring the nesting of structures, pointers, typedef declarations, unions, and enums. By understanding these fundamental concepts and practicing with the provided examples, you're well-equipped to navigate the complexities of C programming, unlocking the potential to create efficient and robust code in this versatile language.