Structures and Unions
Structures and unions! These are fundamental data types in C that help in managing and organizing data efficiently.
Structures (Structs)
Structures allow you to group different types of data into a single unit. This is useful for representing complex data.
Declaration and Definition
To define a structure, use the struct
keyword followed by the structure name and the body containing the members.
#include <stdio.h>
// Define a structure
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Declare and initialize a structure variable
struct Person person1 = {"Alice", 30, 5.5};
printf("Name: %s\n", person1.name); // Outputs: Alice
printf("Age: %d\n", person1.age); // Outputs: 30
printf("Height: %.1f\n", person1.height); // Outputs: 5.5
return 0;
}
Explanation:
Structure Definition:
struct Person
defines a structure withname
,age
, andheight
.Access Members: Use the dot operator
.
to access members of a structure.
Dynamic Allocation with Structures
You can also dynamically allocate memory for structures using pointers.
#include <stdio.h>
#include <stdlib.h>
// Define a structure
struct Person {
char name[50];
int age;
};
int main() {
// Dynamically allocate memory for a structure
struct Person *ptr = (struct Person *)malloc(sizeof(struct Person));
if (ptr == NULL) {
printf("Memory allocation failed\n");
return 1;
}
// Initialize structure members
ptr->age = 25;
snprintf(ptr->name, sizeof(ptr->name), "Bob");
printf("Name: %s\n", ptr->name); // Outputs: Bob
printf("Age: %d\n", ptr->age); // Outputs: 25
free(ptr); // Free the allocated memory
return 0;
}
Explanation:
malloc
allocates memory for the structure.Use
->
to access members of a structure via a pointer.free
releases the allocated memory.
Unions
Unions are similar to structures but differ in how they manage memory. A union allows different data types to share the same memory location.
Declaration and Definition
To define a union, use the union
keyword followed by the union name and its members.
#include <stdio.h>
// Define a union
union Data {
int i;
float f;
char str[20];
};
int main() {
union Data data;
data.i = 10;
printf("Data as integer: %d\n", data.i); // Outputs: 10
data.f = 220.5;
printf("Data as float: %.1f\n", data.f); // Outputs: 220.5
printf("Data as integer: %d\n", data.i); // Outputs: Undefined (overwritten)
snprintf(data.str, sizeof(data.str), "Hello");
printf("Data as string: %s\n", data.str); // Outputs: Hello
printf("Data as float: %.1f\n", data.f); // Outputs: Undefined (overwritten)
return 0;
}
Explanation:
Memory Sharing: All members of a union share the same memory location. Changing one member affects the others.
Size: The size of a union is determined by the size of its largest member.
Comparison: Structures vs. Unions
Structures: Each member has its own memory location. Size is the sum of sizes of all members.
Unions: All members share the same memory location. Size is the size of the largest member.
Using Structures and Unions
Structures: Ideal for cases where you need to group data but keep them distinct.
Unions: Useful when you need to store different types of data but only one at a time.
Example: Combining Structures and Unions
#include <stdio.h>
// Define a structure with a union
struct Employee {
char name[50];
union {
int id;
char code[10];
} identifier;
};
int main() {
struct Employee emp;
snprintf(emp.name, sizeof(emp.name), "John Doe");
emp.identifier.id = 12345;
printf("Name: %s\n", emp.name); // Outputs: John Doe
printf("Employee ID: %d\n", emp.identifier.id); // Outputs: 12345
// If you set code, the id will be overwritten
snprintf(emp.identifier.code, sizeof(emp.identifier.code), "E123");
printf("Employee Code: %s\n", emp.identifier.code); // Outputs: E123
printf("Employee ID (overwritten): %d\n", emp.identifier.id); // Outputs: Undefined (overwritten)
return 0;
}
Explanation:
Combining: A
struct
can contain aunion
to allow different types of data in the same structure.
Summary
Structures group different data types into a single unit.
Unions allow different data types to share the same memory location, useful for memory-efficient storage when only one type is needed at a time.
Dynamic Allocation for structures allows flexible memory management.
Combining structures and unions can be powerful for complex data representations.
Last updated
Was this helpful?