Structures and
Unions in C
Structures and unions are essential features in the C programming language that allow you to organize related data efficiently. They provide a way to define custom data types that can hold different types of data, facilitating the creation of complex data structures. In this article, we will explore structures and unions in C, their syntax, usage, and provide examples to illustrate their functionality.
Structures in C:
A structure is a user-defined data type that allows you to group together different variables under a single name. It enables you to create a composite data type that holds related pieces of information. The variables within a structure are called members or fields.
Syntax for declaring a structure:
```c
struct struct_name {
type member1;
type member2;
// ...
};
```
Here's an example of declaring a structure to represent a person's information:
Syntax:
```c
struct Person {
char name[50];
int age;
float height;
};
```
In this example,
we have declared a structure named "Person" with three members: "name" of type character array, "age" of type integer, and "height" of type float.
To access the members of a structure, you use the dot (.) operator.
Syntax:
```c
struct Person person1;
strcpy(person1.name, "John Doe");
person1.age = 30;
person1.height = 1.75;
```
In this example,
we have declared a structure variable named "person1". We can access its members using the dot operator and assign values to them.
Unions in C:
A union is another user-defined data type that allows you to store different types of data in the same memory location. Unlike structures, which allocate memory for each member separately, unions share the same memory space for all members. This means that a union can hold only one member's value at a time.
Syntax for declaring a union:
```c
union union_name {
type member1;
type member2;
// ...
};
```
Here's an example of declaring a union to represent a shape:
Syntax
```c
union Shape {
int sides;
float radius;
};
```
In this example,
we have declared a union named "Shape" with two members: "sides" of type integer and "radius" of type float.
Accessing the members of a union follows the same syntax as accessing structure members using the dot (.) operator.
Syntax
```c
union Shape shape1;
shape1.sides = 4;
printf("Number of sides: %d\n", shape1.sides);
shape1.radius = 2.5;
printf("Radius: %.2f\n", shape1.radius);
```
In this example,
we have declared a union variable named "shape1". We can access its members using the dot operator and assign values to them. However, note that only one member can be used at a time.
Structures and unions can also be used within other structures or unions, allowing for the creation of nested data structures with hierarchical relationships.
Syntax:
```c
struct Employee {
char name[50];
int age;
union {
float salary;
int hourlyWage;
} pay;
};
```
In this example,
we have defined a structure named "Employee" that contains a union "pay". The union can hold either a salary or an hourly wage, depending on the employee type.
Structures and unions provide flexibility and efficiency in managing complex data in C. They allow you to organize related variables into meaningful collections and efficiently utilize memory. By using structures and unions, you can create custom data types that suit your program's needs, making it easier to work with and manipulate data.
Conclusion:
Structures and unions are powerful tools in the C programming language that allow you to organize and manipulate related data efficiently. They provide a way to define custom data types and create complex data structures. Understanding the syntax and usage of structures and unions is crucial for developing programs that handle complex data effectively. By utilizing structures and unions, you can enhance your programs' readability, maintainability, and memory efficiency.
0 Comments