Use of Structure and Union in - C - 29-May-2024 - SRC
Use of Structure and Union in - C - 29-May-2024 - SRC
C provides two powerful tools for organizing data: structures and unions.
Structures:
● A structure is a user-defined data type that groups variables of different data types
under a single name. These variables are called members of the structure.
● Structures allow you to create complex data objects that represent real-world entities
or concepts.
#include <stdio.h>
// Structure declaration
struct Person {
char name[50];
int age;
float height;
};
int main() {
// Structure variable
struct Person person1;
return 0;
}
Unions:
● A union is similar to a structure, but all members share the same memory location.
This means only one member can have a value at any given time.
● Unions are useful for situations where you need to store different data types that
don't necessarily need to be used simultaneously, saving memory space.
Structures:
● Data Organization: Structures help organize related data into a single unit, making
your code more readable and maintainable.
● Creating Complex Objects: You can create structures that represent real-world
entities like Point (x and y coordinates), Student (name, ID, grade), etc.
Unions:
● Efficient Memory Usage: Unions are space-efficient when you know only one
member will be used at a time.
● Variant Data Types: You can store different data types in a union, depending on the
program's needs.
Q: What is a union in C?
A: A union in C is a user-defined data type that allows storing different data types in the
same memory location.
Unlike a structure, a union can hold only one of its members at a time, saving memory by
sharing the same space for all members.
A: You declare a union using the union keyword followed by the union name and its
members.
#include <stdio.h>
// Union declaration
union Data {
int i;
float f;
char str[20];
};
int main() {
// Union variable
union Data data;
data.f = 220.5;
printf("data.f: %.1f\n", data.f);
strcpy(data.str, "Hello");
printf("data.str: %s\n", data.str);
return 0;
}
A: You would use a structure when you need to group different types of data together and
access all the members independently. Structures are ideal for representing objects with
multiple attributes, such as a record in a database.
A: You would use a union when you need to store different types of data in the same memory
location, but only one type at a time. Unions are useful in situations where you need to save
memory, such as in embedded systems or when dealing with multiple representations of data.
A: No, you cannot access all members of a union simultaneously because they share the same
memory location. Accessing one member will overwrite the other members.
A: Yes, you can access all members of a structure simultaneously because each member has its
own memory space.
A: Yes, structures and unions can be nested within each other. You can have structures inside
unions, unions inside structures, and even structures or unions as members of other structures
or unions. This allows for the creation of complex data types.
#include <stdio.h>
struct Person {
char name[50];
int age;
union {
struct Address address;
char phone[15];
} contact;
};
int main() {
struct Person person1;
return 0;
}
In this example, Person contains a nested structure Address inside a union contact,
demonstrating how complex data types can be created by combining structures and unions.
Remember: