Differences Between Structures and Unions.
Course Title: Mastering C: From Fundamentals to Advanced Programming Section Title: Structures and Unions Topic: Difference between structures and unions.
In the previous topics, we have learned about structures and unions, which are used to group related variables of different data types. While they share some similarities, there are key differences between them. In this topic, we will delve into the differences between structures and unions and provide examples to illustrate their usage.
Similarities between structures and unions
Before we dive into the differences, let's briefly discuss the similarities between structures and unions:
- Both structures and unions can be used to group related variables of different data types.
- Both use a keyword (
struct
for structures,union
for unions) to define the group of variables. - Both can be used to define variables of the same type.
Differences between structures and unions
Here are the main differences between structures and unions:
- Memory allocation: In a structure, each member variable is allocated a separate memory location. In a union, only one memory location is allocated for all member variables, and the variables share this memory location.
Example:
struct employee {
int id;
char name[50];
float salary;
};
union student {
int id;
char name[50];
float salary;
};
In the above example, the employee
structure will allocate separate memory locations for id
, name
, and salary
. However, the student
union will allocate only one memory location for all three variables.
- Accessing member variables: In a structure, you can access all member variables simultaneously. In a union, you can access only one member variable at a time because they share the same memory location.
Example:
struct employee emp;
emp.id = 1;
strcpy(emp.name, "John Doe");
emp.salary = 5000.0;
union student stu;
stu.id = 1;
// You cannot access stu.name or stu.salary until you change the value of stu.id
In the above example, you can access all three member variables of the emp
structure simultaneously. However, you can only access one member variable of the stu
union at a time.
- Memory efficiency: Unions are more memory-efficient than structures because they allocate only one memory location for all member variables.
Example:
typedef struct {
int a;
int b;
int c;
} struct_type;
typedef union {
int a;
int b;
int c;
} union_type;
int main() {
printf("Size of struct_type: %zu bytes\n", sizeof(struct_type));
printf("Size of union_type: %zu bytes\n", sizeof(union_type));
return 0;
}
Output:
Size of struct_type: 12 bytes
Size of union_type: 4 bytes
As you can see, the union_type
uses less memory than the struct_type
.
- Usage: Structures are typically used when you need to store multiple values of different data types that are logically related. Unions are typically used when you need to store one value that can be of different data types.
Example:
typedef struct {
int day;
int month;
int year;
} date;
typedef union {
int color_code;
char* color_name;
} color;
int main() {
date today;
today.day = 25;
today.month = 9;
today.year = 2024;
color fav_color;
fav_color.color_code = 0xFF0000; // red
// You cannot access fav_color.color_name until you change the value of fav_color.color_code
return 0;
}
In the above example, a structure is used to store the date, and a union is used to store the color.
Best practices and conclusion
Here are some best practices to keep in mind when using structures and unions:
- Use structures when you need to store multiple values of different data types that are logically related.
- Use unions when you need to store one value that can be of different data types.
- Be careful when using unions because they can lead to unexpected behavior if not used correctly.
In conclusion, structures and unions are useful data structures in C that can be used to group related variables of different data types. While they share some similarities, there are key differences between them, including memory allocation, accessing member variables, memory efficiency, and usage. By understanding the differences between structures and unions, you can use them effectively in your C programs.
External resources
For more information on structures and unions in C, you can refer to the following resources:
- The C Programming Language (2nd Edition) by Brian Kernighan and Dennis Ritchie
- C Programming Tutorial by tutorialspoint
Leave a comment/ask for help
If you have any questions or need help with any of the concepts discussed in this topic, please leave a comment below. We will be happy to help.
What's next?
In the next topic, we will cover 'Understanding file types: text and binary files' from the section 'File I/O in C'.
Images

Comments