Structures and Unions in C Programming
Introduction:
Structures and unions in C provide a way to group variables of different data types into a single unit. This comprehensive guide aims to provide a thorough understanding of structures and unions, covering basics, syntax, member access, differences, and practical applications.
See Also – while Loop in C Programming
Table of Contents:
- Foundations of Structures in C
- Structure Definition and Purpose
- Declaring and Initializing Structures
- Accessing Structure Members
- Advanced Structure Concepts
- Nested Structures
- Arrays of Structures
- Pointers to Structures
- Foundations of Unions in C
- Union Definition and Purpose
- Declaring and Initializing Unions
- Accessing Union Members
- Differences Between Structures and Unions
- Memory Allocation
- Member Access and Usage
- Use Cases and Considerations
- Practical Applications of Structures and Unions in C
- Record Keeping and Data Organization
- Managing Employee Information
- Handling Variants with Unions
1. Foundations of Structures in C:
Structure Definition and Purpose:
// Structure Definition and Purpose
struct Person {
char name[50];
int age;
float height;
};
Declaring and Initializing Structures:
// Declaring and Initializing Structures
struct Person person1; // Declaration
struct Person person2 = {"Alice", 25, 5.8}; // Initialization
Accessing Structure Members:
// Accessing Structure Members
printf("Name: %s, Age: %d, Height: %.2f\n", person2.name, person2.age, person2.height);
2. Advanced Structure Concepts:
Nested Structures:
// Nested Structures
struct Address {
char street[50];
char city[30];
};
struct Employee {
char name[50];
int empID;
struct Address address;
};
Arrays of Structures:
// Arrays of Structures
struct Point {
int x;
int y;
};
struct Point points[5]; // Array of structures
points[0].x = 1;
points[0].y = 2;
Pointers to Structures:
// Pointers to Structures
struct Student {
char name[50];
int rollNumber;
};
struct Student *studentPtr; // Pointer to a structure
studentPtr = &student1;
printf("Student Name: %s\n", studentPtr->name);
3. Foundations of Unions in C:
Union Definition and Purpose:
// Union Definition and Purpose
union Variant {
int integerValue;
float floatValue;
char stringValue[50];
};
Declaring and Initializing Unions:
// Declaring and Initializing Unions
union Variant value; // Declaration
value.integerValue = 42; // Initialization
Accessing Union Members:
// Accessing Union Members
printf("Integer Value: %d\n", value.integerValue);
4. Differences Between Structures and Unions:
Memory Allocation:
- Structures allocate memory for each member separately.
- Unions allocate memory for the largest member.
Member Access and Usage:
- All members in a structure can be accessed independently.
- Only one member of a union can be accessed at a time.
Use Cases and Considerations:
- Use structures when multiple pieces of data need to be stored and accessed independently.
- Use unions when only one piece of data needs to be used at a time, and there is a need to save memory.
5. Practical Applications of Structures and Unions in C:
Record Keeping and Data Organization:
// Record Keeping and Data Organization
struct Employee {
char name[50];
int empID;
float salary;
};
struct Employee employees[100]; // Array of structures for employee records
Managing Employee Information:
// Managing Employee Information
struct Employee {
char name[50];
int empID;
float salary;
};
struct Employee employee1 = {"Bob", 101, 50000.0};
struct Employee employee2 = {"Alice", 102, 60000.0};
Handling Variants with Unions:
// Handling Variants with Unions
union Variant {
int integerValue;
float floatValue;
char stringValue[50];
};
union Variant value;
value.integerValue = 42;
// Later in the code
printf("Current Value: %d\n", value.integerValue);
Latest posts by Mahesh Wabale (see all)
- AnchorSetup using Docker-Compose - October 18, 2024
- Devops assignment - October 17, 2024
- Deployment of vault HA using MySQL - September 18, 2024