C Structure & Union

What is 'struct' & 'union' in C?

C Structure

In the C programming language, a "structure" (often abbreviated as "struct") is a composite data type that allows you to group together variables of different data types under a single name. It is a user-defined data type used to represent a collection of related values, which can have various data types such as integers, floats, characters, and other data types.

Here's a simple example of a C structure:

struct Point {
    int x;
    int y;
};

In this example, we've defined a structure named "Point" with two integer members, "x" and "y."

Nested Structure:

Structures are often used to group related data together, making it easier to manage and manipulate complex data structures. They are commonly used in C for representing things like geometric points, employee records, and more. Structures can also be nested within other structures to create more complex data structures.

struct Address {
    char street[50];
    char city[30];
    char state[20];
    char zip[10];
};

struct Person {
    char name[50];
    int age;
    struct Address address;
};

In this example, the "Person" structure contains an "Address" structure as one of its members. This demonstrates how structures can be used to create hierarchical or nested data structures in C.

C Union

a union is a composite data type similar to a structure, but with a significant difference. While a structure allows you to group together variables of different data types under a single name, a union allows you to use the same memory location to store different types of data. In a union, all members share the same memory space, and only one of its members can contain a value at any given time.

Here's a simple example of a C union:

union Number {
    int intValue;
    double doubleValue;
};

In this example, the "Number" union has two members, "intValue" and "doubleValue." However, only one of them can be used at a time to store a value.

Nested Union:

You can create nested unions within structures or other unions, just as you can with structures. This allows you to create complex data structures that can represent various data types within one another. Nested unions can be useful in situations where you need to store and manage data with different structures at different levels of hierarchy.

struct Employee {
    int empID;
    char name[50];
    union {
        int hourlyWage;
        double annualSalary;
    } salary;
};

In this example, we have defined a structure "Employee" that contains an integer "empID," a character array "name," and a nested union called "salary." The "salary" union has two members: "hourlyWage" and "annualSalary," which represent different ways to express an employee's compensation.

Note: The structure & union definition ends with a semicolon (;) after the closing brace.

Structure vs Union

Both 'struct' and 'union' are used to create custom data types that can hold multiple variables of different data types. However, they have some key differences in how they allocate memory and store data:

struct union
A 'struct' allocates memory for each member variable independently. This means that the size of a 'struct' is the sum of the sizes of its members. Each member of a 'struct' has its own memory location. A 'union' allocates enough memory to hold the largest member variable. All members share the same memory location, and only one member can hold a value at any given time. The size of a 'union' is determined by the size of its largest member.
All member variables in a 'struct' can hold data simultaneously. Each member has its own memory space, and you can access all of them individually. A 'union' can hold data in only one member at a time. Writing to one member of the 'union' will overwrite the data in the previously used member. Reading from a member will give you the value that was last written to that member.

The '.' Dot Operator

The dot ('.') or period operator is used to access members of a structure or a union. It allows you to access individual variables or fields within a structure or union by specifying the structure/union variable followed by a dot and the name of the member you want to access.

Here's the general syntax for using the dot operator:

structure_variable.member_name

For example, if you have a structure named 'Person':

struct Person {
    char name[50];
    int age;
    float height;
};

You can access its members like this:

struct Person person1;
person1.age = 28;
strcpy(person1.name, "Ayan");
person1.height = 179.83;

In this example, we use the dot operator to access the 'age', 'name', and 'height' members of the 'person1' structure variable.

* If you want to access members of a structure or union through a pointer to the structure or union, you would use the -
arrow operator ('->').

What's Next?

We've now entered the finance section on this platform, where you can enhance your financial literacy.