Twishaexp 7
Twishaexp 7
Experiment No: 7
Title: Structures and unions
COs to be achieved:
CO4: Design modular programs using functions and the use of structure and union.
Theory:
Introduction to Structures
A structure is a user-defined data type in C that groups variables of different types under a single
name. Structures are used when you need to store multiple related pieces of data, such as information
about a student, employee, or product, where each field might have a different data type (e.g.,
integers, floats, and characters).
Example: A structure could be defined to store a student's name, age, and grade.
Example:
struct Student {
char name[50];
int age;
float grade;
};
This defines a structure Student with three members: a string for the name, an integer for the age,
and a float for the grade.
Structure Initialization
Structures can be initialized at the time of declaration or later by assigning values to their members
individually. If initialization during declaration, values for the members are assigned in the same
order as their declaration.
Example:
struct Student student1 = {"John", 20, 85.5};
Syntax:
variable_name.member_name
Example:
printf("Name: %s\n", student1.name);
printf("Age: %d\n", student1.age);
printf("Grade: %.2f\n", student1.grade);
If a structure is pointed to by a pointer, the arrow (->) operator is used to access members.
Example:
struct Student *ptr = &student1;
printf("Name: %s\n", ptr->name);
Array of Structures
An array of structures is used when you want to store multiple instances of a structure. Each element
of the array is a structure.
Example:
struct Student students[3];
students[0].age = 20;
strcpy(students[0].name, "Alice");
students[0].grade = 90.0;
To loop through an array of structures, you can use a for loop:
Introduction to Unions
A union is a user-defined data type similar to a structure, but with one key difference: all members
of a union share the same memory location. This means that at any given time, only one member of
the union can hold a value, making it more memory efficient when you don't need to store multiple
values simultaneously.
Syntax:
union union_name {
data_type member1;
data_type member2;
// more members
};
Example:
union Data {
int i;
float f;
char str[20];
};
In the above example, the Data union can store an integer, a float, or a string, but only one of these
at a time. The memory allocated for all the members of the union is the size of the largest member.
Example:
union Data data;
data.i = 10; // Valid
data.f = 3.14; // Overwrites 'i'
data.str = "Hello"; // Overwrites 'f'
Problem Statements:
Design a C program to manage employee data using structures and unions. The program should
allow the following functionalities:
Requirements:
1. Use a structure to represent an employee with common attributes.
2. Use a union to store role-specific attributes (either for sales or technical employees).
3. Use an enum to differentiate between employee roles (e.g., SALES, TECHNICAL).
4. Implement dynamic memory allocation to store employee records.
5. Provide a menu-driven interface for the user to perform the above operations.
Code :
Output:
Conclusion:
In this experiment we learned about unions and structures and the major differences between them.
We also learned their practical application and importance ina code